Search⌘ K
AI Features

Problem: Top K Frequent Elements

Explore how to identify the top K frequent elements in an integer array by counting frequencies and maintaining a min heap of size K. Learn to implement this efficient solution in Go that runs in O(n log k) time and uses a frequency map and heap operations.

Statement

You are given an integer array nums and an integer k. Your task is to find the k elements that appear most frequently in nums. The result may be returned in any order.

Constraints:

  • 11 \leq nums.length 105\leq 10^5

  • 104-10^4 \leq nums[i] 104\leq 10^4

  • k is in the range ...