Medium
Top K Frequent Elements
Medium
83 submissions
25 coins
+100 XP
Hash Table
Sorting
Problem Description
Return the `k` most frequent values in `nums`, ordered by frequency (highest first). Break ties by the smaller value first.
**Example 1:**
```
Input: nums = [1, 1, 1, 2, 2, 3], k = 2
Output: [1, 2]
```
**Example 2:**
```
Input: nums = [1], k = 1
Output: [1]
```
**Example 3:**
```
Input: nums = [4, 4, 6, 6, 5], k = 2
Output: [4, 6]
```
Constraints
- `1 <= nums.length <= 10^5`
- `1 <= k <=` number of distinct values
Need help?
Connect with expert programmers for real-time collaborative coding, video meetings, and whiteboard sessions via CodeConnect.
Video Call
Whiteboard
Live Coding
Screen Share