Count of pairs from Array with sum equal to twice their bitwise AND | Map to store the occurrence of elements of array ; Function to find the pairs with equal sum and twice the bitwise AND of the pairs ; Loop to find the frequency of elements of array ; Function to find the count such pairs in the array ; If an element occurs more than once then the answer will by incremented by nC2 times ; Driver Code ; Function Call
let mp = new Map ( ) ; function find_pairs ( arr , n ) { let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . set ( arr [ i ] , 1 ) ; } } for ( let [ key , value ] of mp . entries ( ) ) { let count = value ; if ( count > 1 ) { ans += ( ( count * ( count - 1 ) ) / 2 ) ; } } return ans ; } let arr = [ 1 , 2 , 3 , 3 , 4 , 5 , 5 , 7 , 8 ] ; let arr_size = arr . length ; document . write ( find_pairs ( arr , arr_size ) ) ;