Medium · Sorting

Top K Frequent Elements

Given a non-empty integer array nums and an integer k between 1 and the number of distinct values in nums, return the k values that occur most often in nums, in any order; the answer is guaranteed to be unique.

Examples

Example 1

[1,1,1,2,2,3], k=2

Output: [1,2]

Example 2

[4,1,4,2,2,2], k=1

Output: [2]

Rebuild it in the studio

Open every published interview problem, solution, scene, and memory room free, with no account.

More Sorting problems