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