given an array:
array = [16 16 16 22 23 23 23 25 52 52 52]
I want return a list of indices that point to the elements of three repeating numbers.
In this case that would be :
indices = find_sequence(nbr_repeats = 3)
print indices
[0 1 2 4 5 6 8 9 10]
what is the fastest and most elegant algorithm to use in order to implement find_sequence?
Simplest way i know of…keep a track of the first place you saw a number. Keep on going til you find a different number, then if the sequence is long enough, add all the numbers from the start of the sequence til just before the end.
(Of course, you’ll have to check the sequence length after you’re done checking elements, too. I did it by iterating one past the end and just skipping the element check on the last iteration.)