Find the element that appears once | PHP code to find the element that occur only once ; Initialize result ; Iterate through every bit ; Find sum of set bits at ith position in all array elements ; The bits with sum not multiple of 3 , are the bits of element with single occurrence . ; Driver Code
< ? php $ INT_SIZE = 32 ; function getSingle ( $ arr , $ n ) { global $ INT_SIZE ; $ result = 0 ; $ x ; $ sum ; for ( $ i = 0 ; $ i < $ INT_SIZE ; $ i ++ ) { $ sum = 0 ; $ x = ( 1 << $ i ) ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ arr [ $ j ] & $ x ) $ sum ++ ; } if ( ( $ sum % 3 ) != 0 ) $ result |= $ x ; } return $ result ; } $ arr = array ( 12 , 1 , 12 , 3 , 12 , 1 , 1 , 2 , 3 , 2 , 2 , 3 , 7 ) ; $ n = sizeof ( $ arr ) ; echo " The ▁ element ▁ with ▁ single ▁ occurrence ▁ is ▁ " , getSingle ( $ arr , $ n ) ; ? >