Top K Frequent Elements

Read the lesson → understand the pattern → then code. Tutor works offline (built-in teacher); optional Cursor/Gemini keys upgrade chat quality.

Topic refresher: Hashing — Pay Space, Buy Time

Replace nested loops with unordered_map / unordered_set lookups.

unordered_map<int,int> mp;
for (int i = 0; i < n; i++) {
    int need = target - nums[i];
    if (mp.count(need)) return {mp[need], i};
    mp[nums[i]] = i;
}
Hashing

Top K Frequent Elements

Medium
LeetCode
4/8
MediumHashingLeetCode #347

Top K Frequent

Count → bucket by frequency (or heap). Interview favorite: bucket sort O(n).

Examples

Example 1

Input

6 2
1 1 1 2 2 3

Output

1 2
Example 2

Input

1 1
1

Output

1

Stuck? Take a nudge

C++17