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
More Oracle problems
- Add One to a Number Represented as DigitsONSITE INTERVIEW · Seen Jul 2026
- Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Jul 2026
- Merge k Sorted ListsPHONE SCREEN · Seen Jul 2026
- Implement a Queue Using Two StacksPHONE SCREEN · Seen Jul 2026
- First Balanced Removal IndexOA · Seen Dec 2025
- Find Circle NumberOA · Seen Oct 2024
- Create Lexicographically Largest PermutationOA · Seen Sep 2024
- Array Reduction 1OA · Seen Feb 2024