Sum of array elements whose count of set bits are unique | C # program for the approach ; 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
using System ; using System . Collections . Generic ; class solution { static int setBitCount ( int n ) { int ans = 0 ; while ( n > 0 ) { ans += n & 1 ; n >>= 1 ; } return ans ; } static void getSum ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int key = setBitCount ( arr [ i ] ) ; if ( mp . ContainsKey ( key ) ) mp [ key ] += 1 ; else mp [ key ] = 1 ; } for ( int i = 0 ; i < n ; i ++ ) { int key = setBitCount ( arr [ i ] ) ; if ( mp [ key ] == 1 ) ans += arr [ i ] ; } Console . Write ( ans ) ; } public static void Main ( ) { int [ ] arr = { 8 , 3 , 7 , 5 , 3 } ; int n = arr . Length ; getSum ( arr , n ) ; } }