295. The K Most Frequent Values
Print the k values that appear most often, most frequent first. Where two
values appear equally often, print the smaller one first.
The tie rule is there because iteration over a hash map has no defined order, so without it the answer would depend on the language.
Count with a map, then order the distinct values by count descending and by
value ascending. It is guaranteed that k does not exceed the number of
distinct values.
Constraints - `1 ≤ n ≤ 200000` - `1 ≤ k ≤ number of distinct values` - `-1000000000 ≤ a[i] ≤ 1000000000`
Input
The first line contains two integers n and k.
The second line contains n integers.
Output
Print the k most frequent values separated by single spaces, most frequent first, ties by smaller value first.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.