Update frequency
if ( cntFreq . ContainsKey ( arr [ j ] ) ) { var val = cntFreq [ arr [ j ] ] ; cntFreq . Remove ( arr [ j ] ) ; cntFreq . Add ( arr [ j ] , val + 1 ) ; } else { cntFreq . Add ( arr [ j ] , 1 ) ; }