Finding the frequency of each element .
int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . ContainsKey ( arr [ i ] ) == false ) { dist . Add ( arr [ i ] ) ; j ++ ; } if ( freq . ContainsKey ( arr [ i ] ) ) freq [ arr [ i ] ] ++ ; else freq [ arr [ i ] ] = 1 ; }