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