Store the frequency of k
for ( int i = 0 ; i < n ; i ++ ) { if ( freq . ContainsKey ( k [ arr [ i ] ] ) ) { freq [ k [ arr [ i ] ] ] = freq [ k [ arr [ i ] ] ] + 1 ; } else freq . Add ( k [ arr [ i ] ] , 1 ) ; } int sum = 0 ;