I've found several posts about this topic here on stackoverflow, like this one.
... but the main assumption in the suggested solutions is that the array is sorted or ascending or descending...
My problem is that I want to check if there is the sequence of 3 or more consecutive numbers in the array but without sorting the array because it consists of points' coordinates (in the coordinate system) - points are randomly added to the stage during the game... (I'm working in GeoGebra which applet is the stage that I'm adding points to...)
For example, I have the next array:
var array = [0, 4, 6, 5, 9, 8, 9, 12];
The code should only count numbers 4, 6 and 5 as consecutive (although the array isn't sorted), but not 9 and 9 or 9 and 8. Explanation: I've created the code in JS but it works only with sorted array and, besides that, the problem is also that it counts two equal values as well as only two consecutive numbers (like 8 and 9)... My code:
function findConsecutive(array) {
var nmbOfSeq = 0;
for(var i=0; i<array.length; i++) {
for(var j=0; j<array.length; j++) {
if(array[j]==array[i]+1) {
nmbOfSeq+=1;
}
}
}
return nmbOfSeq;
}
Thanks everybody in advance... I'm really stuck with this...