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 . Add ( arr [ i ] , freq [ arr [ i ] ] + 1 ) ; } else { freq . Add ( arr [ i ] , 1 ) ; } }