在数组中查找缺少的数字

时间:2010-11-12 09:26:21

标签: php arrays

我正在尝试在数组中找到每个丢失的数字,如下所示。

Array ( 
  [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 
  [8] => 9 [9] => 10 [10] => 11 [11] => 12 [12] => 13 [13] => 14 [14] => 15 
  [15] => 16 [16] => 17 [17] => 18 [18] => 19 [19] => 20 [20] => 21 [21] => 22 
  [22] => 23 [23] => 24 [24] => 25 [25] => 26 [26] => 27 [27] => 28 [28] => 29 
  [29] => 30 [30] => 31 [31] => 32 [32] => 33 [33] => 34 [34] => 35 [35] => 36 
  [36] => 37 [37] => 38 [38] => 39 [39] => 40 [40] => 41 [41] => 42 [42] => 43 
  [43] => 44 [44] => 45 [45] => 46 [46] => 47 [47] => 48 [48] => 49 [49] => 50 
  [50] => 51 [51] => 52 [52] => 53 [53] => 54 [54] => 55 [55] => 56 [56] => 57 
  [57] => 58 [58] => 59 [59] => 60 [60] => 61 [61] => 62 [62] => 63 [63] => 64 
  [64] => 67 [65] => 68 [66] => 69 
)

此特定数组中缺少数字6566

我的问题如何在PHP的帮助下找出丢失的数字。具体来说,我需要找到的是最少的缺失数字。

为什么:因为那时我可以将该号码作为id分配给成员。

6 个答案:

答案 0 :(得分:53)

您可以将array_diffrange函数用作:

// given array. 3 and 6 are missing.
$arr1 = array(1,2,4,5,7); 

// construct a new array:1,2....max(given array).
$arr2 = range(1,max($arr1));                                                    

// use array_diff to get the missing elements 
$missing = array_diff($arr2,$arr1); // (3,6)

答案 1 :(得分:7)

我假设数字是数组的元素,而不是键。我还假设数字从1开始,而不是0。

$Expected = 1;
foreach ($InputArray as $Key => $Number)
{
   if ($Expected != $Number)
   {
       break;
   }
   $Expected++;
}

echo $Number;

答案 2 :(得分:5)

对于大型排序的唯一数字数组,您可以二进制搜索数组中的最低或最高未使用数字。成本= Log2N。示例:自

以来,可以在16个循环中搜索65536个项目
if ( arr[hi] - arr[lo] > hi - lo )
  ... there are unused numbers in that range ...

所以(我不知道PHP,但可以翻译......):

lo = first entry index
hi = last entry index
if ( arr[hi] - arr[lo] == hi - lo )
  return arr[hi]+1; // no gaps so return highest + 1
do
  {
  mid = (lo + hi) / 2;
  if ( arr[mid] - arr[lo] > mid - lo )   // there is a gap in the bottom half somewhere
    hi = mid; // search the bottom half
  else
    lo = mid; // search the top half
  } while ( hi > lo + 1 ); // search until 2 left
return arr[lo]+1;

答案 3 :(得分:4)

如果给定的输入不是排序的,并且输入的大小很大,那么我们可以在任何编程语言中使用以下逻辑:

算法

  1. 从较大的输入中将较小的块放入内存中
  2. 初始化三个变量,例如min = 0,max = 0和missingIds = []
  3. 从左到右扫描较小的分块输入

    1. 如果在missingIds中找到了scandValue 然后,  从missingIds中弹出scandValue  转到下一个值;
    2. 如果扫描值接近最小值 然后,  找到介于scandValue和min之间的所有缺失数字,将其放入missingIds

      min = scandValue;

    3. 否则,如果扫描值接近最大值 然后,  找到scandValue和max之间的所有缺失数字,将其放入missingIds

      max = scandValue;

  4. 重复上述步骤,直到从左到右扫描大输入

PHP示例

<?php
$largeInput = [40,41,42,43,44,45,1,2,3,4,5,6,7,8,9,10,11,12,13,14,35,36,37,38,39,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,67,68,69,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34];
$missingIds = [];
$min = 0;
$max = 0;
$chunkSize = 10;
$chunkNo = 0;
$currentInput = array_slice($largeInput, $chunkNo, $chunkSize);
while(count($currentInput) > 0) {
    foreach($currentInput as $id) {
        if(in_array($id,$missingIds)) {
            $missingIds = array_diff($missingIds,[$id]);
            continue;
        }
        if($id <= $min) {
            $distMin = $min - $id;
            if($distMin > 2) {
                $tempArr = range($id+1,$min-1);
                $missingIds = array_merge($missingIds, $tempArr);
                $tempArr = [];
            } else if ($distMin > 1) {
                $tempArr = [$id+1];
                $missingIds = array_merge($missingIds, $tempArr);
                $tempArr = [];
            } 
            $min = $id;
        } else if ($id >= $max){
            $distMax = $id - $max;
            if($distMax > 2) {
                $tempArr = range($max+1,$id-1);
                $missingIds = array_merge($missingIds, $tempArr);
                $tempArr = [];
            } else if ($distMax > 1) {
                $tempArr = [$max+1];
                $missingIds = array_merge($missingIds, $tempArr);
                $tempArr = [];
            } 
            $max = $id;
        }   
    }
    $chunkNo++;
    $currentInput = array_slice($largeInput, $chunkNo, $chunkSize);
}
print_r($missingIds);

答案 4 :(得分:2)

//$idArrayMissing = array([0] => 1, [1] => 2, [2] => 4, [3] => 5, [4] => 6, [5] => 7);
$idArrayMissing = array(1, 2, 4, 5, 6, 7);

//$idArrayFull = array([0] => 1, [1] => 2, [2] => 3, [3] => 4, [4] => 5, [5] => 6);
$idArrayFull = array(1, 2, 3, 4, 5, 6);

function gap($arr)
{
   while (list($k, $v) = each($arr))
      if ($k != ($v-1))
         return $k;
   return -1;
}

print "ok:" . gap($idArrayMissing) . "<br/>\n";
print "full:" . gap($idArrayFull) . "<br/>\n";

间隙函数的返回值可以是2个值: -1表示已遍历数组并且没有空闲插槽或 $ k + 1表示第一个空闲时隙位于数组的末尾。

答案 5 :(得分:0)

也可以使用in_array() function这样轻松完成:

// lets say $InputArray has all the data
// lets declare a variable which we will search inside the $InputArray array and lets initialize it with either 0 or 1 or with the minimum value found inside $InputArray

$start_counting = 1;
$max_value = count($InputArray);
  if (!(in_array($start_counting, $InputArray)))
   {
      echo "Value: ".$start_counting." is missing!"."<br>" ;
   }
 else{ 
    if($start_counting <= $max_value -1)    
      {$start_counting++;}
     }
    else  if($start_counting > $max_value -1)
     {
      echo "All missing numbers printed!"
     }  
}