# Find Kth most occurring element in an Array

Given an array of integers arr[] of size N and a number K, the task is to find the Kth most occurring element in this array.

Note: If there are more than one numbers in the array with the same frequency, then both are considered to be at the same level of occurrence. Therefore print both the numbers.

Examples:

Input: arr[] = {1, 2, 2, 2, 4, 4, 4, 5, 5, 5, 5, 5, 7, 7, 8, 8, 8, 8}, K = 1
Output: 5
Explanation:
The occurrence of the elements are as follows:
1 – 1
2 – 3
4 – 3
5 – 5
7 – 2
8 – 4
Clearly, 5 is the most occurring element in the array.

Input: arr[] = {1, 2, 2, 2, 4, 4, 4, 4, 5, 5, 5, 5, 5, 7, 7, 8, 8, 8, 8}, K = 3
Output:

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

Approach: The idea is to use two dictionary data structures to store all the frequencies of the elements.

• Iterate over the given array.
• Find the frequency of all the elements and store it in a dictionary such that the key is the number and the value is its frequency.
• Initialize another dictionary to store the key as the frequency and the value as all the elements with that frequency.
• Finally, since the dictionary is sorted, find the array at (M – K)th position in the dictionary where M is the number of unique elements in the array.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to find K-th ` `// most occurring element in an array ` `#include ` ` `  `using` `namespace` `std; ` ` `  `// Function to find K-th most ` `// occurring element in an array ` `vector<``int``> findKthMostOccurring(vector<``int``> arr, ``int` `K){ ` ` `  `    ``// Initializing a dictionary ` `    ``map<``int``,``int``> d; ` ` `  `    ``// Iterating through the array ` `    ``for` `(``int` `i:arr){ ` ` `  `        ``// If the element is not in ` `        ``// the dictionary, adding it ` `        ``// with the frequency as 1 ` `        ``if` `(d.find(i) == d.end()) ` `            ``d[i] = 1; ` ` `  `        ``// If the element is already ` `        ``// present in the dictionary, ` `        ``// increment its frequency ` `        ``else``{ ` `            ``int` `temp = d[i]; ` `            ``temp += 1; ` `            ``d[i] = temp; ` `        ``} ` `        ``} ` ` `  `    ``// Now, the dictionary signifies ` `    ``// the number of unique elements. ` `    ``// If the count of this is ` `    ``// less than K, then we cant find ` `    ``// the elements whose occurrence is ` `    ``// K-th most occurring. ` `    ``if``(d.size() < K) ` `        ``return` `{}; ` ` `  `    ``// Initializing a new dictionary ` `    ``// to store the elements according ` `    ``// to their frequency ` `    ``map<``int``,vector<``int``> > occu; ` ` `  `    ``// Iterating through the dictionary ` `    ``for` `(``auto` `freq:d){ ` ` `  `        ``// If the element is not in ` `        ``// the dictionary, then store ` `        ``// the element in an array ` `        ``// with key as the frequency ` `        ``if``(occu.find(freq.second) == occu.end()) ` `            ``occu[freq.second].push_back(freq.first); ` ` `  `        ``// Else, add the element to ` `        ``// the array of elements ` `        ``else``{ ` `            ``occu[freq.second].push_back(freq.first); ` `        ``} ` `        ``} ` ` `  `    ``// Since the dictionary is sorted ` `    ``// and not indexed, find (M - K)-th ` `    ``// element where M is the length ` `    ``// of the dictionary ` `    ``K = occu.size() - K; ` ` `  `    ``// Since we for sure know that the ` `    ``// element exists, we iterate ` `    ``// through the dictionary and ` `    ``// return the element ` `    ``for``(``auto` `a:occu){ ` `        ``if``(K == 0) ` `            ``return` `a.second; ` `        ``K -= 1; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``vector<``int``>arr = {1, 4, 4, 4, 2, 2, 2, 5, 5,  ` `                       ``5, 5, 5, 7, 7, 8, 8, 8, 8}; ` `    ``int` `K = 3; ` `    ``vector<``int``> a = findKthMostOccurring(arr, K); ` `    ``cout << ``"["``; ` `    ``for``(``int` `i = 0; i < a.size() - 1; i++) ` `        ``cout << a[i] << ``", "``; ` `    ``cout << a[a.size()-1] << ``"]"``; ` `    ``return` `0; ` `} ` ` `  `// This code is contributed by mohit kumar 29 `

## Python3

 `# Python implementation to find K-th  ` `# most occurring element in an array ` ` `  `# Function to find K-th most  ` `# occurring element in an array ` `def` `findKthMostOccurring(arr, K): ` ` `  `    ``# Initializing a dictionary ` `    ``d ``=` `dict``() ` ` `  `    ``# Iterating through the array ` `    ``for` `i ``in` `arr: ` ` `  `        ``# If the element is not in  ` `        ``# the dictionary, adding it ` `        ``# with the frequency as 1 ` `        ``if` `i ``not` `in` `d: ` `            ``d[i] ``=` `1` ` `  `        ``# If the element is already ` `        ``# present in the dictionary,  ` `        ``# increment its frequency ` `        ``else``: ` `            ``temp ``=` `d[i] ` `            ``temp ``+``=` `1` `            ``d[i] ``=` `temp ` `             `  `    ``# Now, the dictionary signifies  ` `    ``# the number of unique elements. ` `    ``# If the count of this is  ` `    ``# less than K, then we cant find  ` `    ``# the elements whose occurrence is  ` `    ``# K-th most occurring. ` `    ``if``(``len``(d) < K): ` `        ``return` `[] ` `     `  `    ``# Initializing a new dictionary ` `    ``# to store the elements according ` `    ``# to their frequency ` `    ``occu ``=` `dict``() ` `     `  `    ``# Iterating through the dictionary ` `    ``for` `num, freq ``in` `d.items(): ` ` `  `        ``# If the element is not in ` `        ``# the dictionary, then store ` `        ``# the element in an array ` `        ``# with key as the frequency ` `        ``if``(freq ``not` `in` `occu): ` `            ``occu[freq] ``=` `[num] ` ` `  `        ``# Else, add the element to ` `        ``# the array of elements ` `        ``else``: ` `            ``temp ``=` `occu[freq] ` `            ``temp.append(num) ` `            ``occu[freq] ``=` `temp ` `     `  `    ``# Since the dictionary is sorted ` `    ``# and not indexed, find (M - K)-th ` `    ``# element where M is the length ` `    ``# of the dictionary ` `    ``K ``=` `len``(occu) ``-` `K  ` `     `  `    ``# Since we for sure know that the  ` `    ``# element exists, we iterate  ` `    ``# through the dictionary and  ` `    ``# return the element ` `    ``for` `num, a ``in` `occu.items(): ` `        ``if``(K ``=``=` `0``): ` `            ``return` `a ` `        ``K ``-``=` `1` `  `  `# Driver code            ` `if` `__name__ ``=``=` `"__main__"``: ` `    ``arr ``=` `[``1``, ``4``, ``4``, ``4``, ``2``, ``2``, ``2``, ``5``, ``5``, ``5``, ``5``, ``5``, ``7``, ``7``, ``8``, ``8``, ``8``, ``8``] ` `    ``K ``=` `3` `    ``print``(findKthMostOccurring(arr, K)) `

Output:

```[2, 4]
```

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : mohit kumar 29

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.