Algorithms Playbook
← All problems
MediumTop-K →Similar to: Top K Frequent Elements

The k most common values

Problem

Given an integer array nums and an integer k, return the k most frequent elements, in any order. The answer is guaranteed to be unique.

Examples

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

Output: [1, 2]

Input: nums = [7], k = 1

Output: [7]

Hints

    Try it first. Every hint reveals a bit more.

    Solution