Top K Frequent Elements Problem

Top K Frequent Elements Problem — ExecCode Easy DSA Practice

Solve the Top K Frequent Elements problem on ExecCode. Free online easy DSA practice in Hashing. Write and run code in Java, C++, Python — no signup required to run.

Problem description

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

Examples

Input nums = [1, 1, 1, 2, 2, 3, 4, 4, 4, 4], k = 2; Output [4, 1]. Input nums = [1], k = 1; Output [1]. Input nums = [4, 4, 4, 5, 5, 6], k = 2; Output [4, 5]

Constraints

1 ≤ nums.length ≤ 10⁵ k is in the range [1, the number of distinct elements in nums] It is guaranteed that the answer is unique

Practice Top K Frequent Elements free on ExecCode. Browse DSA problems, topic map, and placement guides.