Store the frequencies of all the distinct element in the vector arr
for ( var i = 0 ; i < n ; ) { var count = 1 , x = a [ i ] ; i ++ ; while ( i < n && a [ i ] == x ) { count ++ ; i ++ ; } arr . push ( count ) ; } var m = arr . length ; n = Math . min ( m , k ) ;