Arrays - Find missing numbers in a Sequence

后端 未结 15 1383
小鲜肉
小鲜肉 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 04:45
    function missingNum(nums){
        const numberArray = nums.sort((num1, num2)=>{
          return num1 - num2;
       });
       for (let i=0; i < numberArray.length; i++){
          if(i !== numberArray[i]){
            return i;
          }
       }
     }
     console.log(missingNum([0,3,5,8,4,6,1,9,7]))
    
    0 讨论(0)
  • 2021-02-01 04:47

    Please check below code.....

    function solution(A) {
       var max = Math.max.apply(Math, A);
       if(A.indexOf(1)<0) return 1;
       var t = (max*(max+1)/2) - A.reduce(function(a,b){return a+b});
       return t>0?t:max+1;
    }
    
    0 讨论(0)
  • 2021-02-01 04:48

    If you know that the numbers are sorted and increasing:

    for(var i = 1; i < numArray.length; i++) {
        if(numArray[i] - numArray[i-1] != 1) {
               //Not consecutive sequence, here you can break or do whatever you want
        }
    }
    
    0 讨论(0)
  • 2021-02-01 04:48

    It would be fairly straightforward to sort the array:

    numArray.sort();
    

    Then, depending upon what was easiest for you:

    1. You could just traverse the array, catching sequential patterns and checking them as you go.
    2. You could split the array into multiple arrays of sequential numbers and then check each of those separate arrays.
    3. You could reduce the sorted array to an array of pairs where each pair is a start and end sequence and then compare those sequence start/ends to your other data.
    0 讨论(0)
  • 2021-02-01 04:49

    I use a recursive function for this.

    function findMissing(arr, start, stop) {
    
        var current = start,
            next = stop,
            collector = new Array();
    
        function parseMissing(a, key) {
            if(key+1 == a.length) return;
    
            current = a[key];
            next = a[key + 1];
    
            if(next - current !== 1) {
                collector.push(current + 1);
                // insert current+1 at key+1
                a = a.slice( 0, key+1 ).concat( current+1 ).concat( a.slice( key +1 ) );
                return parseMissing(a, key+1);
            }
    
            return parseMissing(a, key+1);
        }
    
        parseMissing(arr, 0);
        return collector;
    }
    

    Not the best idea if you are looking through a huge set of numbers. FAIR WARNING: recursive functions are resource intensive (pointers and stuff) and this might give you unexpected results if you are working with huge numbers. You can see the jsfiddle. This also assumes you have the array sorted.

    Basically, you pass the "findMissing()" function the array you want to use, the starting number and stopping number and let it go from there.

    So:

    var missingArr = findMissing(sequenceArr, 1, 10);
    
    0 讨论(0)
  • 2021-02-01 04:50

    Try as shown below

    // Find the missing number
    let numArray = [0189459, 0189460, 0189461, 0189463, 0189468];
    let numLen = numArray.length;
    let actLen = Number(numArray[numLen-1])-Number(numArray[0]);
    let  allNumber = [];
    
    for(let i=0; i<=actLen; i++){
      allNumber.push(Number(numArray[0])+i);
    }
    [...allNumber].forEach(ele=>{
      if(!numArray.includes(ele)){
        console.log('Missing Number -> '+ele);
      }
    })
    
    0 讨论(0)
提交回复
热议问题