Find Unique pair in an array with pairs of numbers | C program to find a unique pair in an array of pairs . ; XOR each element and get XOR of two unique elements ( ans ) ; Get a set bit of XOR ( We get the rightmost set bit ) ; Now divide elements in two sets by comparing rightmost set bit of XOR with bit at same position in each element . int x = 0 , y = 0 ; Initialize missing numbers ; XOR of first set in arr [ ] ; XOR of second set in arr [ ] ; Driver code
#include <stdio.h> NEW_LINE void findUniquePair ( int arr [ ] , int n ) { int XOR = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) XOR = XOR ^ arr [ i ] ; int set_bit_no = XOR & ~ ( XOR - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & set_bit_no ) x = x ^ arr [ i ] ; else y = y ^ arr [ i ] ; } printf ( " The ▁ unique ▁ pair ▁ is ▁ ( % d , ▁ % d ) " , x , y ) ; } int main ( ) { int a [ ] = { 6 , 1 , 3 , 5 , 1 , 3 , 7 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; findUniquePair ( a , n ) ; return 0 ; }