EricssonOA #2
Top K Frequent Elements
Estimated MediumJudge-ready
Topics (AI-labelled)HashingStacks & Heaps
Output the k most frequent elements, sorted by frequency in descending order and then by value in ascending order.
The standard input/output conventions and constraints added by this site are given under "Input".
Input
n≤100000, −10000≤x≤10000, 1≤k≤distinct. The input contains n k followed by the array.
Output
Output k integers.
Example 1
Input:
6 2
1 1 1 2 2 3
Output:
1 2
Example 2
Input:
5 5
4 5 2 1 3
Output:
1 2 3 4 5
Example 3
Input:
5 2
-1 -1 2 2 0
Output:
-1 2
Editorial
Loading solution…