How to find the element that appears once in array where other elements appear in pairs with PHP

1 Answer

0 votes
function findElementThatAppearsOnce($arr) { 
    $size = count($arr); 
    
    $element = $arr[0]; 
    for ($i = 1; $i < $size; $i++) {
        $element = $element ^ $arr[$i]; 
    }
    
    return $element; 
}

  

$arr = array(7, 2, 2, 4, 5, 3, 4, 5, 7); 

echo findElementThatAppearsOnce($arr); 
         



/*
run:
         
3
         
*/

 



answered Dec 3, 2021 by avibootz
...