programming-examples/php/Basics/PHP program to find the single number which occurs odd numbers and other numbers occur even number..php

19 lines
384 B
PHP
Raw Permalink Normal View History

2019-11-18 14:44:36 +01:00
<?php
function odd_occurrence($arr)
{
$result = 0;
# Traverse the array
foreach ($arr as &$value)
{
# Xor (exclusive or)
# Bits that are set in $a or $b but not both are set.
$result = $result ^ $value;
}
return $result;
}
$num1 = array(4, 5, 4, 5, 2, 2, 3, 3, 2, 4, 4);
print_r(odd_occurrence($num1)."\n");
?>