Store the frequency of respective counts of set bits
var setbits = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( setbits . has ( bitscount [ arr [ i ] ] ) ) setbits . set ( bitscount [ arr [ i ] ] , setbits . get ( bitscount [ arr [ i ] ] ) + 1 ) else setbits . set ( bitscount [ arr [ i ] ] , 1 ) } var ans = 0 ; setbits . forEach ( ( value , key ) => { ans += value * ( value - 1 ) / 2 ; } ) ; return ans ; }