
PHP Challenges: Find the single element appears once in an array where every element appears twice except for one
PHP Challenges - 1: Exercise-11 with Solution
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)
Sample Solution :
PHP Code :
<?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
Flowchart:

PHP Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a PHP program to find a single element in an array where every element appears three times except for one.
Next: Write a PHP program to add the digits of a positive integer repeatedly until the result has a single digit.
What is the difficulty level of this exercise?
New Content: Composer: Dependency manager for PHP, R Programming