Count all pairs with given XOR | 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 program
function xorPairCount ( arr , n , x ) { let m = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { let curr_xor = x ^ arr [ i ] ; if ( m . has ( curr_xor ) ) result += m . get ( curr_xor ) ; if ( m . has ( arr [ i ] ) ) { m . set ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } else { m . set ( arr [ i ] , 1 ) ; } } return result ; } let arr = [ 2 , 5 , 2 ] ; let n = arr . length ; let x = 0 ; document . write ( " " + xorPairCount ( arr , n , x ) ) ;