I want to find a number of times an integer occurs consecutively in an array. I have found one example doing something similar:
const numbers = [0,0,0,296,296,0,0,296,296,296,0,0,0,0,0,293,293,293,293,293,293,293,293];
let chunks = [];
let prev = 0;
numbers.forEach((current) => {
if ( current - prev != 1 ) chunks.push([]);
// Now we can add our number to the current chunk!
chunks[chunks.length - 1].push(current);
prev = current;
});
chunks.sort((a, b) => b.length - a.length);
console.log('Longest consecutive set:', chunks[0]);
console.log('Size of longest consecutive set:', chunks[0].length);
I just want to get the number of longest consecutive of 0, like:
result: {
key: 0
longestConsecutive: 5
}
Any good solution? Thanks!