Arrays - Find missing numbers in a Sequence

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

    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));
    

提交回复
热议问题