Here a different functional approach in Python (Python looks like Pseudocode). This code works only with Python 3.3+. Otherwise you must replace "return" with "raise StopIteration".

I'm using a generator to yield a tuple with quantity of the element and the element itself. It's more universal. You can use this also for infinite sequences. If you want to get the longest repeated element from the sequence, it must be a finite sequence.

def group_same(iterable): iterator = iter(iterable) last = next(iterator) counter = 1 while True: try: element = next(iterator) if element is last: counter += 1 continue else: yield (counter, last) counter = 1 last = element except StopIteration: yield (counter, last) return

If you have a list like this:

li = [0, 0, 2, 1, 1, 1, 1, 1, 5, 5, 6, 7, 7, 7, 12, 'Text', 'Text', 'Text2']

Then you can make a new list of it:

list(group_same(li))

Then you'll get a new list:

[(2, 0), (1, 2), (5, 1), (2, 5), (1, 6), (3, 7), (1, 12), (2, 'Text'), (1, 'Text2')]

To get longest repeated element, you can use the max function.

gen = group_same(li) # Generator, does nothing until iterating over it grouped_elements = list(gen) # iterate over the generator until it's exhausted longest = max(grouped_elements, key=lambda x: x[0])

Or as a one liner:

max(list(group_same(li)), key=lambda x: x[0])

The function max gives us the biggest element in a list. In this case, the list has more than one element. The argument key is just used to get the first element of the tuple as max value, but you'll still get back the tuple.

In : max(list(group_same(li)), key=lambda x: x[0]) Out: (5, 1)

The element 1 occurred 5 times repeatedly.