Skip to main content
PreparationZone Logo
PreparationZone
Back to Coding

Coding · Medium · Pro

Top k frequent elements in an array

CodingAlgorithmsHeapSenior
Model answerPreview

Approach

  • Count frequencies with HashMap.
  • Min-heap of size k by frequency, or bucket sort when k is small.

Java Solution

Java
public int[] topKFrequent(int[] nums, int k) {
    Map<Integer,Intege…