2011-09-06 40 views
16

我想找到一個簡單的方法來循環(遍歷)數組以查找序列中所有缺少的數字,該數組看起來有點像下面的數字。數組 - 在序列中查找缺少的數字

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

對於陣列上方我需要01894620189464註銷。

有什麼想法?

UPDATE

感謝您的幫助,

對不起肯納貝克無法得到你的函數正確地把工作進行到底。看到你的答案下面的評論。

編輯:這是我從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] 
+0

可以遍歷數組在每兩個元素相比較。 –

回答

21

如果你知道這些數字進行排序,並增加:

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

謝謝Soufiane,是否會捕獲陣列中的每個事件,因爲數組將包含大約100個數字,我將如何記錄不存在的實際數字,再次我可能會變得很厚。 –

+0

@Mark:如果差值大於「1」,那麼'numArray [i]'和'numArray [i-1]'之間的每個數字都不在數組中。 –

+0

是的,你要通過數組中的每一個項目,並找出缺失的數字,當差異是X(X!= 1)時,你缺少的數字是:numArray [i-1] + j(j> 0和j

0

這將是相當簡單的數組進行排序:

numArray.sort(); 

然後,取決於什麼最容易你:

  1. 你可以只是遍歷數組,捕獲序列模式並在你去時檢查它們。
  2. 您可以將數組拆分爲多個連續數字數組,然後檢查每個單獨的數組。
  3. 您可以將已排序的數組減少爲一對數組,其中每對都是開始和結束序列,然後將這些序列開始/結束與其他數據進行比較。
5

注意你的前導零,當陣列是interpreted-

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

變種A = [0189459,0189460,0189461,0189463,0189465]; 警報(無(A))

/*返回值:(陣列) 189462,189464 */

+0

運行此函數時出現以下錯誤 - TypeError 1406:變量indexOf不是函數類型。 –

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。這也假定你有數組排序。

基本上,你通過「findMissing()」函數你想要使用的數組,起始號碼和停止號碼,並讓它從那裏開始。

所以:

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

這裏的一個變種@馬克沃爾特斯的function,如果你知道你的順序應該總是開始於0189455,或其中一些添加到指定的序列的下邊界的能力,例如其他號碼如1

也應該可以調整此代碼以檢查上邊界,但目前它只能查找更低的邊界。

//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; 
 
}
<div id="log"></div> <!-- Just used to display the demo code -->

1

要找到序列中丟失的數量,首先,我們需要對數組排序。然後我們可以確定缺少的是什麼數字。我在這裏提供完整的代碼和一些測試場景。此代碼將只識別缺少的正數,如果您傳遞負值,即使它給出正數。

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