[Python] How to find the longest sequence of equal consecutive elements in a list? [duplicate]

Discussão em 'Python' iniciado por Stack, Junho 20, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working with a list of integers, like this one:

    [1, 1, 2, 2, 2, 3, 3]


    I need to find the length of the longest sequence of the same element appearing consecutively. In this case, the answer should be 3 because the number 2 appears three times in a row.

    I tried using collections.Counter, but it only counts total occurrences, not consecutive ones. Is there a clean way to do this using loops?

    Continue reading...

Compartilhe esta Página