Sum of array elements whose count of set bits are unique | Function to count the number of set bits in an integer N ; Stores the count of set bits ; Iterate until N is non - zero ; Stores the resultant count ; Function to calculate sum of all array elements whose count of set bits are unique ; Stores frequency of all possible count of set bits ; Stores the sum of array elements ; Traverse the array ; Count the number of set bits ; Traverse the array And Update the value of ans ; If frequency is 1 ; Driver Code
function setBitCount ( n ) { let ans = 0 ; while ( n != 0 ) { ans += n & 1 ; n >>= 1 ; } return ans ; } function getSum ( arr , n ) { let mp = new Map ( ) ; let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let key = setBitCount ( arr [ i ] ) ; if ( mp . has ( key ) ) mp . set ( key , mp . get ( key ) + 1 ) ; else mp . set ( key , 1 ) ; } for ( let i = 0 ; i < n ; i ++ ) { let key = setBitCount ( arr [ i ] ) ; if ( mp . has ( key ) && mp . get ( key ) == 1 ) ans += arr [ i ] ; } document . write ( ans ) ; } let arr = [ 8 , 3 , 7 , 5 , 3 ] ; let n = arr . length ; getSum ( arr , n ) ;