Top K Frequent Elements
Given an integer array nums and an integer k, return the k most frequent elements.
To make the output deterministic, sort elements by frequency in descending order. If two elements have the same frequency, the smaller numeric value comes first.
Complete solveTopKFrequentElements. It has the following parameters:
int[] nums: the input array
int k: the number of elements to return
Return an int[] containing the top k elements in the deterministic order described above.
1Example 1
1 appears 3 times and 2 appears 2 times, so they are the two most frequent elements.
2Example 2
Every number appears once, so ties are resolved by smaller value first.
Constraints
Limits and guarantees your solution can rely on.
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
1 <= k <= number of distinct elements in nums