Arrays - Find missing numbers in a Sequence

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

    ES6-Style

    var arr = [0189459, 0189460, 0189461, 0189463, 0189465]; 
    var [min,max] = [Math.min(...arr), Math.max(...arr)];
    var out = Array.from(Array(max-min),(v,i)=>i+min).filter(i=>!arr.includes(i));
    

    Result: [189462, 189464]

    0 讨论(0)
  • 2021-02-01 04:51

    Assuming that there are no duplicates

    let numberArray = [];
    
    for (let i = 1; i <= 100; i++) {
      numberArray.push(i);
    }
    let deletedArray = numberArray.splice(30, 1);
    let sortedArray = numberArray.sort((a, b) => a - b);
    let array = sortedArray;
    
    function findMissingNumber(arr, sizeOfArray) {
      total = (sizeOfArray * (sizeOfArray + 1)) / 2;
      console.log(total);
      for (i = 0; i < arr.length; i++) {
        total -= arr[i];
      }
      return total;
    }
    
    console.log(findMissingNumber(array, 100));
    
    0 讨论(0)
  • 2021-02-01 04:52

    This can now be done easily as a one-liner with the find method:

    const arr = [1,2,3,5,6,7,8,9];
    
    return arr.find((x,i) => arr[i+1]-x > 1) + 1
    
    //4
    
    0 讨论(0)
  • 2021-02-01 04:54
    const findMissing = (numarr) => {
      for(let i = 1; i <= numarr.length; i++) {
          if(i - numarr[i-1] !== 0) {
            console.log('found it', i)
            break;
          } else if(i === numarr.length) console.log('found it', numarr.length + 1)
        }
      };
    
    console.log(findMissing([1,2,3,4,5,6,7,8,9,10,11,12,13,14]))
    
    0 讨论(0)
  • 2021-02-01 04:55

    To find a missing number in a sequence, First of all, We need to sort an array. Then we can identify what number is missing. I am providing here full code with some test scenarios. this code will identify only missing positive number, if you pass negative values even then it gives positive number.

    function findMissingNumber(inputAr) {
      // Sort array
      sortArray(inputAr);
    
      // finding missing number here
      var result = 0;
      if (inputAr[0] > 1 || inputAr[inputAr.length - 1] < 1) {
        result = 1;
      } else {
        for (var i = 0; i < inputAr.length; i++) {
          if ((inputAr[i + 1] - inputAr[i]) > 1) {
            result = inputAr[i] + 1;
          }
        }
      }
      if (!result) {
        result = inputAr[inputAr.length - 1] + 1;
      }
      return result;
    }
    
    function sortArray(inputAr) {
      var temp;
      for (var i = 0; i < inputAr.length; i++) {
        for (var j = i + 1; j < inputAr.length; j++) {
          if (inputAr[j] < inputAr[i]) {
            temp = inputAr[j];
            inputAr[j] = inputAr[i];
            inputAr[i] = temp;
          }
        }
      }
    }
    
    console.log(findMissingNumber([1, 3, 6, 4, 1, 2]));
    console.log(findMissingNumber([1, 2, 3]));
    console.log(findMissingNumber([85]));
    console.log(findMissingNumber([86, 85]));
    console.log(findMissingNumber([0, 1000]));

    0 讨论(0)
  • 2021-02-01 04:59

    let missing = [];
    let numArray = [3,5,1,8,9,36];
    const sortedNumArray = numArray.sort((a, b) => a - b);
    sortedNumArray.reduce((acc, current) => {
      let next = acc + 1;
      if (next !== current) {
        for(next; next < current; next++) {
          missing.push(next);
        }
      }
      return current;
    });

    0 讨论(0)
提交回复
热议问题