Given an array of n numbers and a positive integer k. The problem is to find k numbers with most occurrences, i.e., the top k numbers having the maximum frequency. If two numbers have the same frequency then the larger number should be given preference. The numbers should be displayed in decreasing order of their frequencies. It is assumed that the array consists of k numbers with most occurrences.
Examples:
Input:
arr[] = {3, 1, 4, 4, 5, 2, 6, 1},
k = 2
Output: 4 1
Explanation:
Frequency of 4 = 2
Frequency of 1 = 2
These two have the maximum frequency and
4 is larger than 1.
Input :
arr[] = {7, 10, 11, 5, 2, 5, 5, 7, 11, 8, 9},
k = 4
Output: 5 11 7 10
Explanation:
Frequency of 5 = 3
Frequency of 11 = 2
Frequency of 7 = 2
Frequency of 10 = 10
These four have the maximum frequency and
10 is largest among rest.
Asked in Amazon Interview
Method 1:
- Approach: The thought process should begin from creating a HashMap to store element-frequency pair in the HashMap. HashMap is used to perform insertion and updation in constant time. Then sort the element-frequency pair in decreasing order of frequency. This gives the information about each element and the number of times they are present in the array. To get k elements of the array, print the first k elements of the sorted array.
- Hashmap: HashMap is a part of Java’s collection since Java 1.2. It provides the basic implementation of the Map interface of Java. It stores the data in (Key, Value) pairs. To access a value one must know its key. HashMap is known as HashMap because it uses a technique called Hashing. Hashing is a technique of converting a large String to small String that represents the same String. A shorter value helps in indexing and faster searches. HashSet also uses HashMap internally. It internally uses a link list to store key-value pairs already explained in HashSet in detail and further articles.
More on HashMap >> - Algorithm:
- Create a Hashmap hm, to store key-value pair, i.e. element-frequency pair.
- Traverse the array from start to end.
- For every element in the array update hm[array[i]]++
- Store the element-frequency pair in a vector and sort the vector in decreasing order of frequency.
- Print the first k elements of sorted array.
-
Implementation:
C++
// C++ implementation to find k numbers with most// occurrences in the given array#include <bits/stdc++.h>usingnamespacestd;// comparison function to sort the 'freq_arr[]'boolcompare(pair<int,int> p1, pair<int,int> p2){// if frequencies of two elements are same// then the larger number should come firstif(p1.second == p2.second)returnp1.first > p2.first;// sort on the basis of decreasing order// of frequenciesreturnp1.second > p2.second;}// funnction to print the k numbers with most occurrencesvoidprint_N_mostFrequentNumber(intarr[],intn,intk){// unordered_map 'um' implemented as frequency hash tableunordered_map<int,int> um;for(inti = 0; i < n; i++)um[arr[i]]++;// store the elements of 'um' in the vector 'freq_arr'vector<pair<int,int> > freq_arr(um.begin(), um.end());// sort the vector 'freq_arr' on the basis of the// 'compare' functionsort(freq_arr.begin(), freq_arr.end(), compare);// display the top k numberscout << k <<" numbers with most occurrences are:\n";for(inti = 0; i < k; i++)cout << freq_arr[i].first <<" ";}// Driver program to test aboveintmain(){intarr[] = { 3, 1, 4, 4, 5, 2, 6, 1 };intn =sizeof(arr) /sizeof(arr[0]);intk = 2;print_N_mostFrequentNumber(arr, n, k);return0;}chevron_rightfilter_noneJava
// Java implementation to find k elements with max occurence.importjava.util.*;publicclassKFrequentNumbers {staticvoidprint_N_mostFrequentNumber(int[] arr,intn,intk){Map<Integer, Integer> mp =newHashMap<Integer, Integer>();// Put count of all the distinct elements in Map// with element as the key & count as the value.for(inti =0; i < n; i++) {// Get the count for the element if already// present in the Map or get the default value// which is 0.mp.put(arr[i], mp.getOrDefault(arr[i],0) +1);}// Create a list from elements of HashMapList<Map.Entry<Integer, Integer> > list =newArrayList<Map.Entry<Integer, Integer> >(mp.entrySet());// Sort the listCollections.sort(list,newComparator<Map.Entry<Integer, Integer> >() {publicintcompare(Map.Entry<Integer, Integer> o1,Map.Entry<Integer, Integer> o2){if(o1.getValue() == o2.getValue())returno2.getKey() - o1.getKey();elsereturno2.getValue() - o1.getValue();}});for(inti=0; i<k; i++)System.out.println(list.get(i).getKey());}// Driver Code to test the code.publicstaticvoidmain(String[] args){intarr[] = {3,1,4,4,5,2,6,1};intn = arr.length;intk =2;print_N_mostFrequentNumber(arr, n, k);}}chevron_rightfilter_nonePython3
# Python3 implementation to find k numbers# with most occurrences in the given array# funnction to print the k numbers with# most occurrencesdefpr_N_mostFrequentNumber(arr, n, k):um={}foriinrange(n):ifarr[i]inum:um[arr[i]]+=1else:um[arr[i]]=1a=[0]*(len(um))j=0foriinum:a[j]=[i, um[i]]j+=1a=sorted(a, key=lambdax : x[0],reverse=True)a=sorted(a, key=lambdax : x[1],reverse=True)# display the top k numbersprint(k,"numbers with most occurrences are:")foriinrange(k):print(a[i][0], end=" ")# Driver codeif__name__=="__main__":arr=[3,1,4,4,5,2,6,1]n=8k=2pr_N_mostFrequentNumber(arr, n, k)# This code is contributed by# Shubham Singh(SHUBHAMSINGH10)chevron_rightfilter_none
Output:2 numbers with most occurrences are: 4 1
-
Complexity Analysis:
- Time Complexity: O(d log d), where d is the count of distinct elements in the array. To sort the array O(d log d) time is needed.
- Auxiliary Space: O(d), where d is the count of distinct elements in the array. To store the elements in HashMap O(d) space complexity is needed.
Method 2:
- Approach: Create a HashMap to store element-frequency pair in the HashMap. HashMap is used to perform insertion and updation in constant time. Then use a priority queue to store the element-frequency pair (Max-Heap). This gives the element which has maximum frequency at the root of the Priority Queue. Remove the top or root of Priority Queue K times and print the element. To insert and delete the top of the priority queue O(log n) time is required.
Priority Queue: Priority queues are a type of container adapters, specifically designed such that the first element of the queue is the greatest of all elements in the queue and elements are in non increasing order(hence we can see that each element of the queue has a priority{fixed order}).
More Info about Priority Queue: C++ STL priority_queue - Algorithm :
- Create a Hashmap hm, to store key-value pair, i.e. element-frequency pair.
- Traverse the array from start to end.
- For every element in the array update hm[array[i]]++
- Store the element-frequency pair in a Priority Queue and create the Priority Queue, this takes O(n) time.
- Run a loop k times, and in each iteration remove the top of the priority queue and print the element.
- Implementation:
CPP
// C++ implementation to find k numbers with most// occurrences in the given array#include <bits/stdc++.h>usingnamespacestd;// comparison function defined for the priority queuestructcompare {booloperator()(pair<int,int> p1, pair<int,int> p2){// if frequencies of two elements are same// then the larger number should come firstif(p1.second == p2.second)returnp1.first < p2.first;// insert elements in the priority queue on the basis of// decreasing order of frequenciesreturnp1.second < p2.second;}};// funnction to print the k numbers with most occurrencesvoidprint_N_mostFrequentNumber(intarr[],intn,intk){// unordered_map 'um' implemented as frequency hash tableunordered_map<int,int> um;for(inti = 0; i < n; i++)um[arr[i]]++;// store the elements of 'um' in the vector 'freq_arr'vector<pair<int,int> > freq_arr(um.begin(), um.end());// priority queue 'pq' implemented as max heap on the basis// of the comparison operator 'compare'// element with the highest frequency is the root of 'pq'// in case of conflicts, larger element is the rootpriority_queue<pair<int,int>, vector<pair<int,int> >,compare>pq(um.begin(), um.end());// display the top k numberscout << k <<" numbers with most occurrences are:\n";for(inti = 1; i <= k; i++) {cout << pq.top().first <<" ";pq.pop();}}// Driver program to test aboveintmain(){intarr[] = { 3, 1, 4, 4, 5, 2, 6, 1 };intn =sizeof(arr) /sizeof(arr[0]);intk = 2;print_N_mostFrequentNumber(arr, n, k);return0;}chevron_rightfilter_noneJava
// Java implementation to find k elements with max occurence.importjava.util.*;publicclassKFrequentNumbers {staticvoidprint_N_mostFrequentNumber(int[] arr,intn,intk){Map<Integer, Integer> mp =newHashMap<Integer, Integer>();// Put count of all the distinct elements in Map// with element as the key & count as the value.for(inti =0; i < n; i++) {// Get the count for the element if already present in the Map// or get the default value which is 0.mp.put(arr[i], mp.getOrDefault(arr[i],0) +1);}// Create a Priority Queue to sort based on the count// or on the key if the count is samePriorityQueue<Map.Entry<Integer, Integer> > queue =newPriorityQueue<>((a, b) -> a.getValue().equals(b.getValue()) ?Integer.compare(b.getKey(), a.getKey()) :Integer.compare(b.getValue(), a.getValue()));// Insert the data from the map to the Priority Queue.for(Map.Entry<Integer, Integer> entry : mp.entrySet())queue.offer(entry);// Print the top k elementsfor(inti =0; i < k; i++) {System.out.println(queue.poll().getKey());}}// Driver Code to test the code.publicstaticvoidmain(String[] args){intarr[] = {3,1,4,4,5,2,6,1};intn = arr.length;intk =2;print_N_mostFrequentNumber(arr, n, k);}}// This code is contributed by Shubham Kumar Shahchevron_rightfilter_none
Output:2 numbers with most occurrences are: 4 1
-
Complexity Analysis:
- Time Complexity: O(k log d + d), where d is the count of distinct elements in the array.
To remove the top of priority queue O(log d) time is required, so if k elements are removed then O(k log d) time is required and to traverse the distinct elements O(d) time is required. - Auxiliary Space: O(d), where d is the count of distinct elements in the array.
To store the elements in HashMap O(d) space complexity is needed.
- Time Complexity: O(k log d + d), where d is the count of distinct elements in the array.
Find k most frequent in linear time
References: https://www.careercup.com/question?id=5082885552865280
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Smallest subarray with all occurrences of a most frequent element
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Find the two numbers with odd occurrences in an unsorted array
- Queries to find the XOR of an Array after replacing all occurrences of X by Y
- Count occurrences of the average of array elements with a given number
- Find top k (or most frequent) numbers in a stream
- Find the length of the longest subarray with atmost K occurrences of the integer X
- Delete all occurrences of a given key in a linked list
- Count subarrays with equal number of occurrences of two given elements
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Count occurrences of a string that can be constructed from another given string
- Sum of all distances between occurrences of same characters in a given string
- Count number of occurrences (or frequency) in a sorted array
- Maximum distance between two occurrences of same element in array
- Remove all occurrences of any element for maximum array sum
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Maximize occurrences of values between L and R on sequential addition of Array elements with modulo H
- Array sum after replacing all occurrences of X by Y for Q queries
- Find the word with most anagrams in a given sentence
- Find the most frequent digit without using array/string

