数组 - 在序列中查找缺少的数字

时间:2011-09-06 09:50:39

标签: javascript arrays loops sequence

我正在尝试找到一种简单的方法来循环(迭代)数组以查找序列中所有缺少的数字,该数组看起来有点像下面的数字。

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

对于上面的数组,我需要01894620189464注销。

更新:这是我在Soufiane的回答中使用的确切解决方案。

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]

更新

这是使用.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);

16 个答案:

答案 0 :(得分:28)

如果您知道这些数字已经排序并且正在增加:

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

答案 1 :(得分:5)

观察前导零,解释数组时它们将被删除 -

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

(A返回[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]; 警报(不存在(A))

/ *返回值:(数组) 189462,189464 * /

答案 2 :(得分:3)

要查找序列中缺少的数字,首先,我们需要对数组进行排序。然后我们可以确定缺少的数字。我在这里提供了一些测试场景的完整代码。此代码将仅识别缺失的正数,如果您传递负值,即使它然后它给出正数。

&#13;
&#13;
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]));
&#13;
&#13;
&#13;

答案 3 :(得分:1)

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

答案 4 :(得分:1)

尝试如下所示

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

答案 5 :(得分:0)

对数组进行排序非常简单:

numArray.sort();

然后,取决于最简单的方法:

  1. 您可以遍历数组,捕获顺序模式并随时检查它们。
  2. 您可以将数组拆分为多个连续数组,然后检查每个单独的数组。
  3. 您可以将已排序的数组减少为一对数组,其中每对是一个开始和结束序列,然后将这些序列的开始/结束与其他数据进行比较。

答案 6 :(得分:0)

我为此使用递归函数。

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

如果您正在查看大量数字,那么这不是最佳选择。公平警告:递归函数是资源密集型(指针和东西),如果您处理大量数据,这可能会给您带来意想不到的结果。你可以看到jsfiddle。这也假设你已经对数组进行了排序。

基本上,你传递了&#34; findMissing()&#34;运行你想要使用的数组,起始编号和停止编号,然后从那里开始。

所以:

var missingArr = findMissing(sequenceArr, 1, 10);

答案 7 :(得分:0)

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

答案 8 :(得分:0)

请检查下面的代码.....

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

答案 9 :(得分:0)

ES6风格

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

结果:[[189462,189464]

答案 10 :(得分:0)

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

答案 11 :(得分:0)

假设没有重复

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

答案 12 :(得分:0)

现在可以很容易地通过查找方法以单行的方式完成:

const arr = [1,2,3,5,6,7,8,9];

return arr.find((x,i) => arr[i+1]-x > 1) + 1

//4

答案 13 :(得分:0)

这是查找数组中缺失数字的最有效和最简单的方法。只有一个循环,复杂度为 O(n)。

/**
 * 
 * @param {*} item Takes only the sorted array
 */
function getAllMissingNumbers(item) {
  let first = 0;
  let second = 1;
  let currentValue = item[0];
  const container = [];
  while (first < second && item[second]) {
    if ((item[first] + 1) !== item[second]) { // Not in sequence so adds the missing numbers in an array
      if ((currentValue + 1) === item[second]) { // Moves the first & second pointer
        first = second;
        second++;
        currentValue = item[first];
      } else { // Adds the missing number between two number
        container.push(++currentValue);
      }
    } else { // Numbers are in sequence so just moves the first & second pointer
      first = second;
      second++;
      currentValue = item[first];
    }
  }
  return container;
}

console.log(getAllMissingNumbers([0189459, 0189460, 0189461, 0189463, 0189465].sort( (a, b) => a - b )));
console.log(getAllMissingNumbers([-5,2,3,9]));

答案 14 :(得分:-1)

这里是@Mark Walters docs的一个变体,它增加了为序列指定下边界的功能,例如,如果你知道你的序列总是应该从{{1开始}或其他一些数字,例如0189455

还应该可以调整此代码以检查上边界,但目前它只能查找下边界。

&#13;
&#13;
1
&#13;
//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;
}
&#13;
&#13;
&#13;

答案 15 :(得分:-1)

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