If my array looks like
[0, 2, 4, 10, 10, 10, 10, 2, 5, 3, 2, 10, 10, 5, 7, 4, 10, 10, 10, 10]
How do I determine the number of times a sequence of 10's
occurs with at least 3 repetitions.
In this example, the output would be 2
because there are 2 sequences of 10's with four 10's in each sequence.
const values = [0, 2, 4, 10, 10, 10, 10, 2, 5, 3, 2, 10, 10, 5, 7, 4, 10, 10, 10, 10];
const MAX = 10;
const threshold = 3;
let count= 0;
let numberInSeq = 0;
values.forEach(x => {
if (x === MAX) {
numberInSeq++;
} else {
if (numberInSeq >= threshold) {
count++
}
numberInSeq = 0;
}
})
return count;
This code should work as intended, but I am open to suggestions for optimizing it further.
Thank you!