How do I find a maximum number of consecutive non-zero values in the array?

I couldn’t resist doing this - javascript version

const s = [8,0,4,2,0,0,3,7,1,6,0,9]
const n = Math.max(...s.reduce((a, i) => (i?a[a.length-1]++:a.push(0),a),[0]))
console.log(n)

or

const s = [8,0,4,2,0,0,3,7,1,6,0,9]
const n = Math.max(...s.reduce((a, i) => (i?a[0]++:a.unshift(0),a),[0]))
console.log(n)

or even shorter

const s = [8,0,4,2,0,0,3,7,1,6,0,9]
const n = Math.max(...s.reduce((a, i) => (i?a[0]++:a=[0,...a],a),[0]))
console.log(n)
3 Likes