Q:

Write a PHP program to find the single element appears once in an array where every element appears twice except for one

0

Write a PHP program to find the single element appears once in an array where every element appears twice except for one.

Input : array(5, 3, 0, 3, 0, 5, 7, 7, 9)

All Answers

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

<?php
function single_number($arr)
{
  $result = 0;
  
       for($i=0; $i<sizeof($arr); $i++)
       {
          $result =  $result ^ $arr[$i];   
          
       }
    return $result;
}
$num = array(5, 3, 0, 3, 0, 5, 7, 7, 9);
print_r(single_number($num)."\n")
?>

Sample Output:

9

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