Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

nums =
k =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

Count frequencies, then retrieve the k most frequent values.

Approach

  1. Count each number with a hash map.
  2. Use bucket sort: buckets[f] stores numbers that appear f times.
  3. Scan buckets from high frequency to low, collecting numbers until you have k.

Why it works

Bucket indices represent frequency, so scanning from the end yields elements in descending frequency without sorting all keys.

Code
Loading...
Complexity
Time: O(n)
Space: O(n)
Solution
Updated: 2026-02-23

Idea

Count frequencies, then retrieve the k most frequent values.

Approach

  1. Count each number with a hash map.
  2. Use bucket sort: buckets[f] stores numbers that appear f times.
  3. Scan buckets from high frequency to low, collecting numbers until you have k.

Why it works

Bucket indices represent frequency, so scanning from the end yields elements in descending frequency without sorting all keys.

Code
Loading...
Complexity
Time: O(n)
Space: O(n)