Count pairs with equal Bitwise AND and Bitwise OR value | Function to count pairs in an array whose bitwise AND equal to bitwise OR ; Store count of pairs whose bitwise AND equal to bitwise OR ; Stores frequency of distinct elements of array ; Traverse the array ; Increment the frequency of arr [ i ] ; Traverse map ; Driver Code
function countPairs ( arr , N ) { var cntPairs = 0 ; var mp = new Map ( ) ; for ( var 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 ) ; } mp . forEach ( ( value , key ) => { cntPairs += parseInt ( ( value * ( value - 1 ) ) / 2 ) ; } ) ; return cntPairs ; } var arr = [ 1 , 2 , 3 , 1 , 2 , 2 ] ; var N = arr . length ; document . write ( countPairs ( arr , N ) ) ;