Last active
January 17, 2016 04:33
-
-
Save rafaelbernard/82e952b81b1992206835 to your computer and use it in GitHub Desktop.
This file contains 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 | |
function solution($A) { | |
// write your code in PHP5.5 | |
$count = count($A); | |
if ($count < 1) return 1; | |
$end = $count + 1; | |
$ordered = range(1, $end); | |
$flip_A = array_flip($A); | |
$flip_ordered = array_flip($ordered); | |
$diff = array_diff_key($flip_ordered, $flip_A); | |
if(count($diff) == 0) return 1; | |
$key_flip = current($diff); | |
$value = $ordered[$key_flip]; | |
return $value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment