Arrays - Find missing numbers in a Sequence

后端 未结 15 1385
小鲜肉
小鲜肉 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:01

    Watch your leading zeroes, they will be dropped when the array is interpreted-

    var A= [0189459, 0189460, 0189461, 0189463, 0189465]

    (A returns [189459,189460,189461,189463,189465])

    function absent(arr){
        var mia= [], min= Math.min.apply('',arr), max= Math.max.apply('',arr);
        while(min<max){
            if(arr.indexOf(++min)== -1) mia.push(min);
        }
        return mia;
    }
    

    var A= [0189459, 0189460, 0189461, 0189463, 0189465]; alert(absent(A))

    /* returned value: (Array) 189462,189464 */

    0 讨论(0)
  • 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]<br>"
    html += "Its  lowerBoundary is \"0189455\"<br>"
    html += "The following numbers are missing from the numArray:<br>"
    html += findMissingNumbers(numArray, numArrayLowerBoundary);
    html += "<br><br>"
    html += "simpleArray = [3, 5, 6, 7, 8, 10, 11, 13]<br>"
    html += "Its  lowerBoundary is \"1\".<br>"
    html += "The following numbers are missing from the simpleArray:<br>"
    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;
    }
    <div id="log"></div> <!-- Just used to display the demo code -->

    0 讨论(0)
  • 2021-02-01 05:08
    const findMissing = (arr) => {
    const min = Math.min(...arr);
    const max = Math.max(...arr);
    // add missing numbers in the array
    let newArr = Array.from(Array(max-min), (v, i) => {
        return i + min
    });
    // compare the full array with the old missing array
    let filter = newArr.filter(i => {
        return !arr.includes(i)
    })
    return filter;
    };
    
    0 讨论(0)
提交回复
热议问题