Skip to content

Instantly share code, notes, and snippets.

@hackjoy
Created February 6, 2013 23:38
Show Gist options
  • Save hackjoy/4726924 to your computer and use it in GitHub Desktop.
Save hackjoy/4726924 to your computer and use it in GitHub Desktop.
Calculates and returns the element in a list that has repeated the most times in succession.
def longest_repetition(list):
best_length = 0
best_element = None
current_length = 0
current_element = None
for e in list:
if e != current_element:
current_element = e
current_length = 1
else:
current_length += 1
if current_length > best_length:
best_length = current_length
best_element = current_element
return best_element
# print longest_repetition([1, 2, 2, 3, 3, 3, 2, 2, 1])
# 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment