Skip to content

Instantly share code, notes, and snippets.

@Mark-H
Created July 30, 2012 22:26
Show Gist options
  • Save Mark-H/3211054 to your computer and use it in GitHub Desktop.
Save Mark-H/3211054 to your computer and use it in GitHub Desktop.
Find missing item in array 1-100
<?php
/* Generate random array */
$m = rand(1,100);
echo '<p>Answer: '.$m.'</p>';
$list = array();
for ($i=1;$i<=100;$i++) $list[] = $i;
unset($list[array_search($m,$list)]);
function findMissing ($list, $idx = 1) {
if ($idx >= 98) return 'too much to handle';
if (array_search($idx, $list, true) === false) {
return $idx;
}
$idx++;
return findMissing($list, $idx);
}
echo '<p>Missing ' . findMissing($list) . ' in the array.</p>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment