Given an array of integers **arr[]** of size **N** and a number **K**, the task is to find the **K ^{th}** 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:

**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 <bits/stdc++.h> ` ` ` `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 ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

## Recommended Posts:

- Sum of all even occurring element in an array
- First element occurring k times in an array
- Find the sum of all highest occurring elements in an Array
- Find even occurring elements in an array of limited range
- Find Array formed by adding each element of given array with largest element in new array to its left
- Find just strictly greater element from first array for each element in second array
- Find last element after deleting every second element in array of n integers
- Find an element in array such that sum of left array is equal to sum of right array
- Find element in array that divides all array elements
- Sum of all minimum occurring elements in an Array
- Find if array has an element whose value is half of array sum
- Print the most occurring character in an array of strings
- Two odd occurring elements in an array where all other occur even times
- GCD of elements occurring Fibonacci number of times in an Array
- Find the maximum element in the array other than A
_{i} - Find Second largest element in an array | Set 2
- Find Second largest element in an array
- Find extra element in the second array
- Find the maximum possible value of last element of the Array
- Find an element in Bitonic array

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.