Created
June 18, 2015 14:13
-
-
Save yuklia/47c41998136865ad9ea7 to your computer and use it in GitHub Desktop.
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 | |
function calculate($arr, $len){ | |
$inArray = function($ar, $value){ | |
for ($i = 0; $i < count($ar); $i++) { | |
if ($ar[$i] == $value) { | |
return true; | |
} | |
} | |
return false; | |
}; | |
$i = 0; | |
$missing = []; | |
$duplicates = []; | |
$min = $arr[$i]; | |
$max = $arr[$i]; | |
for (; $i < count($arr);) { | |
$current = $arr[$i]; | |
if ($arr[$i] < $min) { | |
$min = $arr[$i]; | |
} | |
if ($arr[$i] > $max) { | |
$max = $arr[$i]; | |
} | |
if(isset($duplicates[$current])){ | |
$duplicates[$current] = $duplicates[$current] + 1; | |
}else{ | |
$duplicates[$current] = 1; | |
} | |
$i++; | |
} | |
for ($j = $min; $j <= $max; $j++) { //range | |
if (!$inArray($arr, $j)) { | |
$missing[] = $j; | |
} | |
} | |
echo sprintf("Range is %s to %s", $min, $max) ."</br>"; | |
echo "Missing Numbers: </br>"; | |
foreach($missing as $num){ | |
echo $num."</br>"; | |
} | |
echo "Duplicate Numbers: </br>"; | |
foreach($duplicates as $key=>$num){ | |
if($num>=2){ | |
echo sprintf('%d appears %d times',$key,$num) ."</br>"; | |
} | |
} | |
} | |
$test = [-3, 1, -5, 10, 3, -5, 0, 3, 1, 1]; | |
calculate($test, count($test)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment