0

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...

4
  • can you copy and sort and find, or must it be difficult and complicated to implement? Commented Jul 26, 2016 at 17:23
  • 1
    "points are randomly added to the stage during the game" - might perhaps make more sense to check at that point ... maybe by keeping a sorted helper array. Commented Jul 26, 2016 at 17:23
  • I've got help for looping through x-coordinates array that I've changed a little bit and adjusted to my needs, so now it returns the exact indices of x-coordinates that are equal (say I have 3 or more points on the line with equation x = some vale...). Also, I know how to 'catch' appropriate y-coordinates (of those points with the equal x-coordinates) and now I have to check if there are 3 or more consecutive y-coordinates because it means that I have three in a row and the game is over... Commented Jul 26, 2016 at 17:31
  • ... If I would sort that array another problem would emerge: how to determine, once again, after sorting which three y-coordinates are consecutive in order to highlight at the scene the three in a row... Commented Jul 26, 2016 at 17:37

1 Answer 1

0

I suggest to get first the index of a group of three and then build an array with the groups of the indices.

var array = [0, 4, 6, 5, 9, 8, 9, 12, 1, 2, 3, 4],
    indices = [],
    result = [];

array.forEach(function (a, i, aa) {
    var temp;
    if (i < 2) { return; }
    temp = aa.slice(i - 2, i + 1);
    temp.sort(function (a, b) { return a - b; });
    temp[0] + 1 === temp[1] && temp[1] + 1 === temp[2] && indices.push(i);
});

indices.forEach(function (a, i, aa) {
    if (aa[i - 1] + 1 === a) {
        result[result.length - 1].push(array[a]);
        return;
    }
    result.push(array.slice(a - 2, a + 1));
});

console.log(indices);
console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.