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 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 Frequency of4= 2 Frequency of1= 2 These two have the maximum frequency and4is larger than1. Input : arr[] = {7, 10, 11, 5, 2, 5, 5, 7, 11, 8, 9}, k = 4 Output : 5 11 7 10

Asked in Amazon Interview

**Method 1:** Using hash table, we create a frequency table which stores the frequency of occurrence of each number in the given array. In the hash table we define **(x, y)** tuple, where **x** is the key(number) and **y** is its frequency in the array. Now we traverse this hash table and create an array **freq_arr[]** which stores these (number, frequency) tuples. Sort this **freq_arr[]** on the basis of the conditions defined in the problem statement. Now, print the first **k** numbers of this **freq_arr[]**.

`// C++ implementation to find k numbers with most ` `// occurrences in the given array ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// comparison function to sort the 'freq_arr[]' ` `bool` `compare(pair<` `int` `, ` `int` `> p1, pair<` `int` `, ` `int` `> p2) ` `{ ` ` ` `// if frequencies of two elements are same ` ` ` `// then the larger number should come first ` ` ` `if` `(p1.second == p2.second) ` ` ` `return` `p1.first > p2.first; ` ` ` ` ` `// sort on the basis of decreasing order ` ` ` `// of frequencies ` ` ` `return` `p1.second > p2.second; ` `} ` ` ` `// funnction to print the k numbers with most occurrences ` `void` `print_N_mostFrequentNumber(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `// unordered_map 'um' implemented as frequency hash table ` ` ` `unordered_map<` `int` `, ` `int` `> um; ` ` ` `for` `(` `int` `i = 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' function ` ` ` `sort(freq_arr.begin(), freq_arr.end(), compare); ` ` ` ` ` `// display the the top k numbers ` ` ` `cout << k << ` `" numbers with most occurrences are:\n"` `; ` ` ` `for` `(` `int` `i = 0; i<k; i++) ` ` ` `cout << freq_arr[i].first << ` `" "` `; ` `} ` ` ` `// Driver program to test above ` `int` `main() ` `{ ` ` ` `int` `arr[] = {3, 1, 4, 4, 5, 2, 6, 1}; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `int` `k = 2; ` ` ` `print_N_mostFrequentNumber(arr, n, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

2 numbers with most occurrences are: 4 1

Time Complexity: O(dlogd), where **d** is the count of distinct elements in the array.

Auxiliary Space: O(d), where **d** is the count of distinct elements in the array.

**Method 2:** Create the array **freq_arr[]** as described in **Method 1** of this post. Now, build the max heap using elements of this **freq_arr[]**. The root of the max heap should be the most frequent number and in case of conflicts the larger number gets the preference. Now remove the top **k** numbers of this max heap. C++ STL priority_queue has been used as max heap.

`// C++ implementation to find k numbers with most ` `// occurrences in the given array ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// comparison function defined for the priority queue ` `struct` `compare ` `{ ` ` ` `bool` `operator()(pair<` `int` `, ` `int` `> p1, pair<` `int` `, ` `int` `> p2) ` ` ` `{ ` ` ` `// if frequencies of two elements are same ` ` ` `// then the larger number should come first ` ` ` `if` `(p1.second == p2.second) ` ` ` `return` `p1.first < p2.first; ` ` ` ` ` `// insert elements in the priority queue on the basis of ` ` ` `// decreasing order of frequencies ` ` ` `return` `p1.second < p2.second; ` ` ` `} ` `}; ` ` ` `// funnction to print the k numbers with most occurrences ` `void` `print_N_mostFrequentNumber(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `// unordered_map 'um' implemented as frequency hash table ` ` ` `unordered_map<` `int` `, ` `int` `> um; ` ` ` `for` `(` `int` `i = 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 root ` ` ` `priority_queue<pair<` `int` `, ` `int` `>, vector<pair<` `int` `, ` `int` `> >, ` ` ` `compare> pq(um.begin(), um.end()); ` ` ` ` ` `// display the the top k numbers ` ` ` `cout << k << ` `" numbers with most occurrences are:\n"` `; ` ` ` `for` `(` `int` `i = 1; i<= k; i++) ` ` ` `{ ` ` ` `cout << pq.top().first << ` `" "` `; ` ` ` `pq.pop(); ` ` ` `} ` `} ` ` ` `// Driver program to test above ` `int` `main() ` `{ ` ` ` `int` `arr[] = {3, 1, 4, 4, 5, 2, 6, 1}; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `int` `k = 2; ` ` ` `print_N_mostFrequentNumber(arr, n, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

2 numbers with most occurrences are: 4 1

Time Complexity: O(klogd), where **d** is the count of distinct elements in the array.

Auxiliary Space: O(d), where **d** is the count of distinct elements in the array.

**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.

## Recommended Posts:

- Find the two numbers with odd occurrences in an unsorted array
- Maximum distance between two occurrences of same element in array
- Count occurrences of the average of array elements with a given number
- Count number of occurrences (or frequency) in a sorted array
- Find the longest sub-array having exactly k odd numbers
- Find last k digits in product of an array numbers
- Find four missing numbers in an array containing elements from 1 to N
- Find first k natural numbers missing in given array
- Find Unique pair in an array with pairs of numbers
- Find all numbers that divide maximum array elements
- Find the nearest smaller numbers on left side in an array
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Find original array from encrypted array (An array of sums of other elements)
- Find an element in array such that sum of left array is equal to sum of right array
- Given pairwise sum of n numbers, find the numbers