You are given n integers and a number k. Rank the distinct values by how many times they appear in the array: a value with a higher count ranks earlier, and when two values have the same count the smaller value ranks earlier. This ordering is a strict total order, so the ranking is unique.
Output the first k values in this ranking.
Line 1: two integers n and k.
Line 2: n space-separated integers, the array.
One line: the top k values in the described order, space-separated.
Example 1
Input
6 2 1 1 1 2 2 3
Expected
1 2
Explanation
Counts: 1 appears 3 times, 2 appears 2 times, 3 appears once. Ranked by count descending: 1, 2, 3. The top 2 are 1 and 2.
Example 2
Input
5 3 4 4 -1 -1 7
Expected
-1 4 7
Explanation
Counts: 4 and -1 each appear twice, 7 appears once. On the tie between 4 and -1, the smaller value -1 comes first, so the order is -1, 4, 7.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →