Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Top K Frequent Elements

XPChallenge Points: 20
levelLevel: Medium

You are given an array of integers nums.
Your task is to find the k values that appear the most frequently in the array.

  • The result can be returned in any order.

  • If multiple numbers have the same frequency, any of them may appear in the result, as long as exactly k values are returned.

Example 1:

Input: nums = [4,4,4,5,5,6], k = 2

Output: [4,5]

Explanation:

4 appears 3 times, 5 appears 2 times, and 6 appears once → Top 2 are 4 and 5.

Example 2:

Input: nums = [9,9,8,8,7,7], k = 3

Output: [7,8,9]

Explanation:

All appear exactly twice → any order is valid.

Example 3:

Input: nums = [10], k = 1

Output: [10]

Explanation:

Only one number exists.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result