values and a number count. Find the count distinct values that occur in the array most often and return them in any order.
The answer is guaranteed to be unique: the value ranked count-th by frequency occurs strictly more often than any value left out of the answer.
Aim for better than O(n log n) time.values=[4, 7, 4, -2, 7, 4, 9], count=2 → [4, 7]
4 appears three times, 7 twice, -2 and 9 once each. The two most frequent are 4 and 7.
values=[3, 3, 0, 0, 0, 5], count=1 → [0]
0 is the most frequent, with three occurrences.
values=[6, 1, 6, 1, 2], count=3 → [6, 1, 2]
count equals the number of distinct values, so all three are returned.
values ≤ 10⁵values[i] ≤ 10⁴count ≤ number of distinct values in valuescount most frequent values is uniquely determinedvalues=[4, 7, 4, -2, 7, 4, 9], count=2
[4, 7]
Run checks the first 3 tests in your browser. Submit checks all 22 tests on the server, hidden ones included.