Traverse the array and update frequencies in hash
for ( int i = 0 ; i < N ; i ++ ) { if ( hash . containsKey ( arr [ i ] ) ) { hash . put ( arr [ i ] , hash . get ( arr [ i ] ) + 1 ) ; } else { hash . put ( arr [ i ] , 1 ) ; } } for ( int i = 0 ; i < N ; i ++ ) {