javascript - find sequence of numbers in array and alert highest number -


i have array looks this:

[1,2,3,4,5,6,8,10,12,13,14,15,20,21,22,23,24,25,26,27,28,29,30] 

the highest count on 1 of found sequences be: 10

my goal loop through array , identify sequences of numbers, find length of highest sequence exists.

so, based on array above, length of longest sequence "10"

does know of quick , easy script find this?

ok, think found short way of doing (only 1 line for loop):

var arr = [1,2,3,4,5,6,8,10,12,13,14,15,20,21,22,23,24,25,26,27,28,29,30]; var res = new array(); res[0] = 0;  for(var i=1;i<arr.length;i++) res[i] = (arr[i] == arr[i-1] + 1) ? (res[i-1] + 1) : 0;  var maxlength = math.max.apply({},res); 

this gives (10) result. if need (11) (which makes more sense) change 0 1 in loop.

jsfiddle link: http://jsfiddle.net/gezza/8/


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -