Count all pairs with given XOR | C # program to Count all pair with given XOR value x ; Returns count of pairs in arr [ 0. . n - 1 ] with XOR value equals to x . ; create empty map that stores counts of individual elements of array . ; If there exist an element in map m with XOR equals to x ^ arr [ i ] , that means there exist an element such that the XOR of element with arr [ i ] is equal to x , then increment count . ; Increment count of current element ; return total count of pairs with XOR equal to x ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int xorPairCount ( int [ ] arr , int n , int x ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int curr_xor = x ^ arr [ i ] ; if ( m . ContainsKey ( curr_xor ) ) result += m [ curr_xor ] ; if ( m . ContainsKey ( arr [ i ] ) ) { var val = m [ arr [ i ] ] ; m . Remove ( arr [ i ] ) ; m . Add ( arr [ i ] , val + 1 ) ; } else { m . Add ( arr [ i ] , 1 ) ; } } return result ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 5 , 2 } ; int n = arr . Length ; int x = 0 ; Console . WriteLine ( " Count ▁ of ▁ pairs ▁ with ▁ given ▁ XOR ▁ = ▁ " + xorPairCount ( arr , n , x ) ) ; } }