Problem · Array

Top K Frequent Elements with Larger-Value Tie Break

Learn this problem
MediumOracle logoOracleFULLTIMEONSITE INTERVIEW

Problem 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^9
  • 1 <= k <= the number of distinct values in nums

More Oracle problems

drafts saved locally
public int[] topKFrequent(int[] nums, int k) {
  // Write your code here.
}
nums[1,1,1,2,2,3]
k2
expected[1,2]
checking account