Finding the frequency of each element .
int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { dist [ j ++ ] = arr [ i ] ; if ( ! freq . containsKey ( arr [ i ] ) ) freq . put ( arr [ i ] , 1 ) ; else freq . put ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; }