Find the first missing number in a sequence of numbers

前端 未结 5 735
猫巷女王i
猫巷女王i 2021-01-28 13:57

It\'s the second day I try to find a solution for this problem.

I have an array.

$datas;

$datas[0]; // 8000
$datas[1]; // 8001
$datas[2]; // 8003
$datas         


        
5条回答
  •  后悔当初
    2021-01-28 14:00

    With something like this, it's pretty simple to just iterate through the array with a foreach loop.

    foreach ($datas as $key=>$val) { 
      if ( $val +1 != $datas[$key+1] ) { 
        echo $val+1; // this is the first free value in the sequence
        }
      }
    

提交回复
热议问题