Top K Frequent Elements
Problem statement
Given an integer array nums and an integer k, return the k distinct values with the highest frequencies.
Order the answer by decreasing frequency. If two selected values have equal frequency, place the smaller value first.
Function
topKFrequent(nums: int[], k: int) → int[]Examples
Example 1
nums = [1,1,1,2,2,3]k = 2return = [1,2]1 occurs three times and 2 occurs twice.
Example 2
nums = [4,4,-1,-1,2]k = 2return = [-1,4]The selected values tie in frequency, so the smaller value comes first.
Constraints
1 <= nums.length <= 100000.-10^9 <= nums[i] <= 10^9.1 <= k <=the number of distinct values.