mirror of
https://github.com/kamranahmedse/developer-roadmap.git
synced 2026-10-25 09:34:00 +08:00
* refactor: clean to roadmap content * refactor: move shared helpers into scripts/lib
685 B
685 B
Top K Frequent Elements
Given an array and a number k, return the k most frequent elements. You could sort by frequency, but the optimal approach uses bucket sort. Since no element can appear more times than the length of the array, you can create buckets indexed by frequency and scan from the top. This problem bridges hash maps and sorting, and introduces the idea that the constraints of a problem often suggest a faster algorithm.
Visit the following resources to learn more: