backbenchcode
← all problemsnext: Shortest Transformation
#11MediumArrays & Strings · 60% acceptance

Top K Frequent Elements

Hash MapHeapBucket Sort

Given an integer array `nums` and an integer `k`, return the `k` most frequent elements, in any order.

Counting occurrences and ranking them is daily work in a lab. Here you make it efficient.

Examples

Input: nums = [1,1,1,2,2,3], k = 2

Output: [1, 2]

Constraints

Hints

solution.pyPython 3
1
2
3
4
5
6
Test console

// hit Run to execute the sample tests

// Interactive editor preview. Live code execution arrives with your early-access cohort.