Phase 1: Count frequencies using a hash map. Phase 2: Find the elements with highest frequency using a heap.
For phase 2, you can either: - Use a min-heap of size (keep most frequent) - Use a max-heap of all elements and extract times The min-heap approach is , better when is small. Time: . Space: .