I have an array and want to find all neighboring matches, count them, and then return the count. This needs to be done in a loop using .map() so that I don't need to store memory of what was counted beyond the current element. The number of current elements will be used to reserve enough spaces for each group of elements.
array = [ball, batter, batter, amount, amount, github, github, github, account, account, account, account, account, account, account, github, github, github]
An example of the desired results from this array would be: on the first loop it would return 1, second loop would return 2, then 2, then 3, then 7, then 3
This count will serve as a variable to reserve space like so:
number to reserve: count
Through each loop, the variable count
will be updated with the current elements count. The counting loop will not stop until the next element does not match the current element, and the variable count
will only be available once all consecutive matches are found. Therefore, if I put console.log(count)
at the end of the function, I would get each number output individually.