Find the two non | This function sets the values of * x and * y to non - repeating elements in an array arr [ ] of size n ; Xor all the elements of the array all the elements occuring twice will cancel out each other remaining two unnique numbers will be xored ; Bitwise & the sum with it ' s ▁ 2' s Complement Bitwise & will give us the sum containing only the rightmost set bit ; sum1 and sum2 will contains 2 unique elements elements initialized with 0 box number xored with 0 is number itself ; Traversing the array again ; Bitwise & the arr [ i ] with the sum Two possibilities either result == 0 or result > 0 ; If result > 0 then arr [ i ] xored with the sum1 ; If result == 0 then arr [ i ] xored with sum2 ; Print the the two unique numbers ; Driver Code
function UniqueNumbers2 ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum = ( sum ^ arr [ i ] ) ; } sum = ( sum & - sum ) ; let sum1 = 0 ; let sum2 = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( ( arr [ i ] & sum ) > 0 ) { sum1 = ( sum1 ^ arr [ i ] ) ; } else { sum2 = ( sum2 ^ arr [ i ] ) ; } } document . write ( " " + " " + sum1 + " " + sum2 ) ; } let arr = [ 2 , 3 , 7 , 9 , 11 , 2 , 3 , 11 ] ; let n = arr . length ; UniqueNumbers2 ( arr , n ) ;