Find maximum element in array and fill frequency map .
for ( int i = 0 ; i < n ; i ++ ) { arrMax = Math . max ( arrMax , arr [ i ] ) ; if ( freq . containsKey ( arr [ i ] ) ) { freq . put ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; } else { freq . put ( arr [ i ] , 1 ) ; } }