# Find k numbers with most occurrences in the given array

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 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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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;
}
```

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;
}
```

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.

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.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
3.2 Average Difficulty : 3.2/5.0
Based on 15 vote(s)