Find the two non | Java Program for above approach ; 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
public class UniqueNumbers { public static void UniqueNumbers2 ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = ( sum ^ arr [ i ] ) ; } sum = ( sum & - sum ) ; int sum1 = 0 ; int sum2 = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( ( arr [ i ] & sum ) > 0 ) { sum1 = ( sum1 ^ arr [ i ] ) ; } else { sum2 = ( sum2 ^ arr [ i ] ) ; } } System . out . println ( " The ▁ non - repeating ▁ elements ▁ are ▁ " + sum1 + " ▁ and ▁ " + sum2 ) ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 2 , 3 , 7 , 9 , 11 , 2 , 3 , 11 } ; int n = arr . length ; UniqueNumbers2 ( arr , n ) ; } }