admin管理员组文章数量:1391969
I have an array that looks like 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 one of the found sequences would be: 10
My goal is to loop through the array and identify the sequences of numbers, then find the length of the highest sequence that exists.
So, based on the array above, the length of the longest sequence would be "10"
Does anyone know of quick and easy script to find this?
I have an array that looks like 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 one of the found sequences would be: 10
My goal is to loop through the array and identify the sequences of numbers, then find the length of the highest sequence that exists.
So, based on the array above, the length of the longest sequence would be "10"
Does anyone know of quick and easy script to find this?
Share Improve this question edited Jun 13, 2014 at 23:46 Web_Designer 74.7k93 gold badges210 silver badges267 bronze badges asked Jul 20, 2011 at 21:39 jakejake 3175 silver badges19 bronze badges 4- 1 Isn't the length of the longest sequence 11 (20-30)? – carlosfigueira Commented Jul 20, 2011 at 21:45
- 1 This sounds like homework. Have you attempted anything yet? – Luke Commented Jul 20, 2011 at 21:50
-
Is
2,4,6
classed as 1 sequence? – Ash Burlaczenko Commented Jul 20, 2011 at 21:56 - BTW, are you going to accept anything as answer? ;) – Mo Valipour Commented Jul 20, 2011 at 22:09
3 Answers
Reset to default 4OK, I think I found a very short way of doing this (only 1 line for the 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 you (10) as the result. if you need (11) (which makes more sense) change the 0 to 1 in the for loop.
jsFiddle link: http://jsfiddle/gEzzA/8/
You don't need jQuery for this.
function longestSeq(arr) {
var len = 0, longestLen = -1, prev = null;
for (var i = 0; i < arr.length; ++i) {
if (prev == null || arr[i] - 1 === prev)
++len;
else {
if (len > longestLen) longestLen = len;
len = 1;
}
}
return longestLen > len ? longestLen : len;
}
What that does is keep track of how long it's been since a "break" has been seen. Each time a break is seen, it checks whether the longest so far is shorter than the last good run.
Here's the solution in pseudo code...
First, setup another array with the same number of elements and initialised to zero, to use as counters...
Array01:=[1,2,3,4,5,6,8,10,12,13,14,15,20,21,22,23,24,25,26,27,28,29,30]
Array02:=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Now the logic for filling in the counters...
FOR i:=0 TO LastElement DO
WHILE (Array01[i+1]-Array01[i]=1) AND (i<LastElement) DO Inc(Array02[i]);
Now to scan who's got the highest sequence score...
which:=0; Value:=Array02[0];
FOR i:=0 TO LastElement DO
IF Array02[i]>Value THEN BEGIN Value:=Array02[i]; Which:=i; END;
So, at the end of this the highest sequence is held by Array element "Which" and the count is "Value"!
本文标签: javascriptfind sequence of numbers in array and alert highest numberStack Overflow
版权声明:本文标题:javascript - find sequence of numbers in array and alert highest number - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744706856a2620889.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论