Another alternative: use RLE (run-length-estimation), borrowed originally from a very simple data compressor of the same name.
Here's the code, albeit in the R
language. (For non-users, "1L" just forces integer-class, x[-k] means all of vector x except index 'k' )
Here's the base package code for the function rle(x)
:
y <- x[-1L] != x[-n] ;
i <- c(which(y | is.na(y)), n) ;
structure(list(lengths = diff(c(0L, i)), values = x[i]), class = "rle")
then add a line to find max(lengths[values==0])