Skip to content

Instantly share code, notes, and snippets.

@rafaelbernard
Created January 17, 2016 05:36
Show Gist options
  • Save rafaelbernard/ef8a766b24ee3abedf06 to your computer and use it in GitHub Desktop.
Save rafaelbernard/ef8a766b24ee3abedf06 to your computer and use it in GitHub Desktop.
<?php
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($A) {
// write your code in PHP5.5
$A = array_unique($A);
$count = count($A);
if ($count < 1) return 1;
$end = $count + 1;
$ordered = range(1, $end);
$flip_A = array_flip($A);
//print_r($fliped_A);
$flip_ordered = array_flip($ordered);
//print_r($flipper_ordered);
//$diff = array_diff($ordered, $A);
$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