Medium · Hash Maps

Top K Frequent Elements (Hash maps)

Given a non-empty integer array a and an integer k between 1 and its number of distinct values, return the k values that occur most often in a, in any order. The set of top k values is unique, with no tie at the cutoff.

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

Core interview problems are free with a full runner and lessons; Pro opens the complete library.

More Hash Maps problems