Arrays - Find missing numbers in a Sequence

后端 未结 15 1390
小鲜肉
小鲜肉 2021-02-01 04:08

I\'m trying to find an easy way to loop (iterate) over an array to find all the missing numbers in a sequence, the array will look a bit like the one below.

var nu

15条回答
  •  借酒劲吻你
    2021-02-01 05:02

    Here's a variant of @Mark Walters's function which adds the ability to specify a lower boundary for your sequence, for example if you know that your sequence should always begin at 0189455, or some other number like 1.

    It should also be possible to adjust this code to check for an upper boundary, but at the moment it can only look for lower boundaries.

    //Our first example array.
    var numArray = [0189459, 0189460, 0189461, 0189463, 0189465];
    //For this array the lowerBoundary will be 0189455
    var numArrayLowerBoundary = 0189455;
    
    //Our second example array.
    var simpleArray = [3, 5, 6, 7, 8, 10, 11, 13];
    //For this Array the lower boundary will be 1
    var simpleArrayLowerBoundary = 1;
    
    //Build a html string so we can show our results nicely in a div
    var html = "numArray = [0189459, 0189460, 0189461, 0189463, 0189465]
    " html += "Its lowerBoundary is \"0189455\"
    " html += "The following numbers are missing from the numArray:
    " html += findMissingNumbers(numArray, numArrayLowerBoundary); html += "

    " html += "simpleArray = [3, 5, 6, 7, 8, 10, 11, 13]
    " html += "Its lowerBoundary is \"1\".
    " html += "The following numbers are missing from the simpleArray:
    " html += findMissingNumbers(simpleArray, simpleArrayLowerBoundary); //Display the results in a div document.getElementById("log").innerHTML=html; //This is the function used to find missing numbers! //Copy/paste this if you just want the function and don't need the demo code. function findMissingNumbers(arrSequence, lowerBoundary) { var mia = []; for (var i = 0; i < arrSequence.length; i++) { if (i === 0) { //If the first thing in the array isn't exactly //equal to the lowerBoundary... if (arrSequence[i] !== lowerBoundary) { //Count up from lowerBoundary, incrementing 1 //each time, until we reach the //value one less than the first thing in the array. var x = arrSequence[i]; var j = lowerBoundary; while (j < x) { mia.push(j); //Add each "missing" number to the array j++; } } //end if } else { //If the difference between two array indexes is not //exactly 1 there are one or more numbers missing from this sequence. if (arrSequence[i] - arrSequence[i - 1] !== 1) { //List the missing numbers by adding 1 to the value //of the previous array index x times. //x is the size of the "gap" i.e. the number of missing numbers //in this sequence. var x = arrSequence[i] - arrSequence[i - 1]; var j = 1; while (j < x) { mia.push(arrSequence[i - 1] + j); //Add each "missing" num to the array j++; } } //end if } //end else } //end for //Returns any missing numbers, assuming that lowerBoundary is the //intended first number in the sequence. return mia; }

提交回复
热议问题