programming-examples/php/Basics/PHP program to find a single number in an array that doesn't occur twice..php

20 lines
403 B
PHP
Raw Normal View History

2019-11-18 14:44:36 +01:00
<?php
function single_number($arr)
{
$result = $arr[0];
for($i=1;$i<sizeof($arr);$i++)
{
$result = $result ^ $arr[$i];
}
return $result;
}
$arr1 = array(5, 3, 4, 3, 4);
$arr2 = array(3, 2, 5, 2, 1, 5, 3);
print_r($arr1);
print_r('Single Number: '.single_number($arr1)."\n");
print_r($arr2);
print_r('Single Number: '.single_number($arr2)."\n");
?>