Created
July 30, 2012 22:26
-
-
Save Mark-H/3211054 to your computer and use it in GitHub Desktop.
Find missing item in array 1-100
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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