Medium · Hash Maps

Find top K frequent elements

Given a non-empty integer array a and an integer k between 1 and its number of distinct values, return k values that occur most often in a, in any order; if values tie at the cutoff, any of those tied values may be chosen.

Examples

Example 1

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

Output: [1, 2]

Example 2

[4,4,5,5,5,6], k=2

Output: [5, 4]

Rebuild it in the studio

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

More Hash Maps problems