K
K
kuzubina2021-06-16 22:49:06
JavaScript
kuzubina, 2021-06-16 22:49:06

How to find the maximum number of repetitions in an array of consecutive ones?

There is an array of this kind. It is necessary to calculate the maximum number of repeating elements in a row. For example, in this case, you need to calculate how many maximum 1 goes in a row (there are 4 repetitions).
const arr = [1, 5, 99, 1, 1, 1, 2, 8, 1, 1 ,1 ,1]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-06-16
@kuzubina

Math.max(...arr.reduce((acc, n, i, a) => (
  n !== a[i - 1] && acc.push(0),
  acc[acc.length - 1]++,
  acc
), [ 0 ]))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question