Q:

Write a PHP program to find a missing number(s) from an array

0

Write a PHP program to find a missing number(s) from an array.

Input : 1,2,3,6,7,8

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

function missing_number($num_list)
{
 // construct a new array
$new_arr = range($num_list[0],max($num_list));                                                    
// use array_diff to find the missing elements 
return array_diff($new_arr, $num_list);

}
print_r(missing_number(array(1,2,3,6,7,8)));
print_r(missing_number(array(10,11,12,14,15,16,17)));
?>

Sample Output:

Array                                                                                
(                                                                                    
    [3] => 4                                                                         
    [4] => 5                                                                         
)                                                                                    
Array                                                                                
(                                                                                    
    [3] => 13                                                                        
)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now