I would like to find the length of streaks within the following array:
states = [:read, :read, :read, :unread, :unread, :read, :read, :read, :read]
The array has either
:unread, and I want the length of each streak of
states, this would become:
streak_lengths = [3, 3, 3, 2, 2, 4, 4, 4, 4]
The array opens with three
:read elements, and we label each of those as being part of a
3 streak, it then has two
:unread elements, so they're each labelled with a
2 streak, and then finally we have a streak of four read messages, so they're each labelled with a
What is an elegant, efficient and readable way to solve the above problem?
Is it a recursion problem? While I can solve this problem, I feel that it hints at a way of tackling it that I'm not familiar with. It hints that perhaps it's something best solved with recursion.
(For the benefit of the "possibly duplicate" flag applied to this: both threads actually have slightly different discussions. In addition from a search perspective, you would only find the other thread if you search for repeating characters, this the answer to how you detect repeating array elements. Finally there are a load of excellent answers on here, removing this question wouldn't make the ecosystem richer, just poorer)