Problem · Array
Top K Frequent Elements with Larger-Value Tie Break
Learn this problemProblem statement
Given an integer array nums and an integer k, return the k most frequent distinct values.
Order the result by decreasing frequency. When two values have the same frequency, the larger value must appear first.
Function
topKFrequent(nums: int[], k: int) → int[]Examples
Example 1
nums = [1,1,1,2,2,3]k = 2return = [1,2]Example 2
nums = [4,4,1,1,2,2]k = 2return = [4,2]All three values occur twice, so the larger values 4 and 2 come first.
Constraints
1 <= nums.length <= 200000-10^9 <= nums[i] <= 10^91 <= k <=the number of distinct values innums