Count the frequency of every element
for ( int i = 0 ; i < n ; ++ i ) { if ( hash . containsKey ( a [ i ] ) ) { hash . replace ( a [ i ] , hash . get ( a [ i ] ) + 1 ) ; } else { hash . put ( a [ i ] , 1 ) ; } }