Data Structures19 sections · 729 units
Open in Course

Problem - Top K Frequent Elements

Frequency + heap

Given an integer array numsnums and an integer kk, return the kk most frequent elements. Example: nums=[1,1,1,2,2,3]nums = [1,1,1,2,2,3], k=2k = 2 Output: [1,2][1,2] (1 appears 3 times, 2 appears 2 times) This combines frequency counting (hash maps) with the k-th element pattern (heaps).