Skip to interview prep
Apexed

46. Top K Frequent Elements

MediumArrays & hashing

Return the k most frequent values in nums. Order them by frequency (highest first); break ties by smaller value first.

Example 1

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

Constraints

  • 1 ≤ k ≤ number of distinct values

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 4 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Top K Frequent Elements — coding interview practice — Apexed