Prepin
Log in

Leetcode problem 347: Top K Frequent Elements, can be found at https://leetcode.com/problems/top-k-frequent-elements/. Follow-up question: What is the approach for dealing with a scenario that includes millions of elements?

Approach / Explanation

This problem requires you to develop an algorithm that identifies the most frequently occurring elements in an input array, limited to a specified number k . For instance, given the array [1,1,2,2,3,3,3,4,4,4,4] with k=2 , the expected output is [4, 3] , as 4 and 3 are the two elements that appear most frequently. The frequency of an element is determined by how many times it appears in the array. Additionally, the ability to process large datasets is a crucial part of this challenge. The problem highlights the need for a solution that can efficiently manage "a case containing millions of elements," suggesting that an optimal approach should have a time complexity lower than O(n^2).

Suggested Answer

A useful approach is to combine a data structure known as a priority queue with frequency mapping, such as a hash map. We begin by traversing the input array to record the frequency of each element in a hash map. Next, we utilize a min-heap priority queue, where the frequency count serves as the key and the element is the value, to determine the top k most frequent elements. The implementation in Python would appear as follows: from collections import Counter import heapq class Solution : def topKFrequent ( self , nums : List [ int ] , k : int ) - > List [ int ] : # convert to frequency mapping count = Counter ( nums ) # use min heap to get top k frequent elements return heapq . nlargest ( k , count . keys ( ) , key = count . get )

Alternative Answer

Another method that resembles the initial approach involves utilizing a bucket sort technique, where a) You create a frequency map to tally the occurrence of each element in the array. b) You set up an array of lists (buckets) such that the index of each list corresponds to the frequency of elements. Specifically, bucket[i] will hold a list of elements that appear i times in the array. c) You iterate through the bucket array from the end and gather the elements into the result until its size reaches k . This method maintains a linear time complexity, that is, O(n). from collections import Counter , defaultdict class Solution : def topKFrequent ( self , nums : List [ int ] , k : int ) - > List [ int ] : # create a counter for the numbers count = Counter ( nums ) # create a bucket with frequency as index and append corresponding elements bucket = defaultdict ( list ) for n , freq in count . items ( ) : bucket [ freq ] . append ( n ) # traverse through the bucket res = [ ] for freq in range ( len ( nums ) , - 1 , - 1 ) : res += bucket [ freq ] if len ( res ) >= k : return res [ : k ] In these methods, we assume that Python’s list implementation can manage millions of elements. For extremely large input datasets or in a programming language where the standard list-like structures cannot accommodate millions of elements, alternative data structures such as a Trie or a B-tree might be employed to enhance efficiency.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Microsoft

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.