Calculate the frequency
for ( int i = 0 ; i < chars . Length ; i ++ ) { if ( freq . ContainsKey ( chars [ i ] ) ) { freq [ chars [ i ] ] ++ ; } else { freq . Add ( chars [ i ] , freq . GetValueOrDefault ( chars [ i ] , 0 ) + 1 ) ; } }