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