Arrays - Find missing numbers in a Sequence

2020-05-18 16:25发布

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 numArray = [0189459, 0189460, 0189461, 0189463, 0189465];

For the array above I would need 0189462 and 0189464 logged out.

UPDATE : this is the exact solution I used from Soufiane's answer.

var numArray = [0189459, 0189460, 0189461, 0189463, 0189465];
var mia= [];

    for(var i = 1; i < numArray.length; i++) 
    {     
        if(numArray[i] - numArray[i-1] != 1) 
        {         
            var x = numArray[i] - numArray[i-1];
            var j = 1;
            while (j<x)
            {
                mia.push(numArray[i-1]+j);
                j++;
            }
        }
    }
alert(mia) // returns [0189462, 0189464]

UPDATE

Here's a neater version using .reduce

var numArray = [0189459, 0189460, 0189461, 0189463, 0189466];
var mia = numArray.reduce(function(acc, cur, ind, arr) {
  var diff = cur - arr[ind-1];
  if (diff > 1) {
    var i = 1;
    while (i < diff) {
      acc.push(arr[ind-1]+i);
      i++;
    }
  }
  return acc;
}, []);
console.log(mia);

14条回答
beautiful°
2楼-- · 2020-05-18 17:02

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

查看更多
ゆ 、 Hurt°
3楼-- · 2020-05-18 17:03

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 */

查看更多
别忘想泡老子
4楼-- · 2020-05-18 17:03
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;
};
查看更多
叼着烟拽天下
5楼-- · 2020-05-18 17:07
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]))
查看更多
ゆ 、 Hurt°
6楼-- · 2020-05-18 17:10

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]

查看更多
够拽才男人
7楼-- · 2020-05-18 17:11

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));
查看更多
登录 后发表回答