Problem
You get an integer array nums and an integer k. Return the k values that appear most often in nums.
You can return the values in any order. For each input, only one correct set of k values exists.
Example 1
- Input:
- nums = [4,4,4,6,6,9], k = 2
- Output:
- [4,6]
- Explanation:
- The value 4 appears three times, and 6 appears two times. The value 9 appears only one time.
Example 2
- Input:
- nums = [-2,5,-2,7,5,-2], k = 1
- Output:
- [-2]
- Explanation:
- The value -2 appears three times. No other value appears that often.
Example 3
- Input:
- nums = [3,1,2,1,3,1,2,3,3], k = 3
- Output:
- [3,1,2]
Constraints
- 1 <= nums.length <= 10^5
- -10^4 <= nums[i] <= 10^4
- 1 <= k <= the number of different values in nums
- Only one correct answer exists.
Follow-up
Find a solution that runs faster than O(n log n) time, where n is the length of nums.
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Top K Frequent Words
Coding · Array · Bucket Sort · Counting
- K Closest Points to Origin
Coding · Array · Divide and Conquer · Geometry
- Kth Largest Element in an Array
Coding · Array · Divide and Conquer · Heap (Priority Queue)
- Majority Element
Coding · Array · Boyer–Moore Majority Vote Algorithm · Counting
Sources · 2 references
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 9 records
Amazon
Leetcode 347. Top K Frequent Elements
Reported Oct 1, 2026 · 7 source reports
MID_LEVEL, SENIOR, STAFF
Source snapshot: Nov 12, 2026
View sourceGoogle
Leetcode 347. Top K Frequent Elements
Reported Aug 23, 2026 · 5 source reports
JUNIOR, MID_LEVEL, SENIOR
Source snapshot: Nov 12, 2026
View sourceMeta
Leetcode 347. Top K Frequent Elements
Reported Oct 1, 2026 · 35 source reports
MID_LEVEL, SENIOR, STAFF
Source snapshot: Nov 12, 2026
View sourceMicrosoft
Leetcode 347. Top K Frequent Elements
Reported Mar 30, 2026 · 2 source reports
MID_LEVEL
Source snapshot: Nov 12, 2026