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



Below is the implementation of the above approach: 

filter_none

edit
close

play_arrow

link
brightness_4
code

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

edit
close

play_arrow

link
brightness_4
code

// Java implementation to find K-th
// most occurring element in an array
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to find K-th most
// occurring element in an array
static ArrayList<Integer>findKthMostOccurring(
       ArrayList<Integer> arr, int K)
{
     
    // Initializing a dictionary
    HashMap<Integer, Integer> d = new HashMap<>();
 
    // Iterating through the array
    for(int i = 0; i < arr.size(); i++)
    {
         
        // If the element is not in
        // the dictionary, adding it
        // with the frequency as 1
        if (!d.containsKey(arr.get(i)))
            d.put(arr.get(i), 1);
 
        // If the element is already
        // present in the dictionary,
        // increment its frequency
        else
        {
            int temp = d.get(arr.get(i));
            temp += 1;
            d.put(arr.get(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 new ArrayList<Integer>();
 
    // Initializing a new dictionary
    // to store the elements according
    // to their frequency
    HashMap<Integer,
            ArrayList<Integer>> occu = new HashMap<Integer,
                                                   ArrayList<Integer>>();
 
    // Iterating through the dictionary
    for(Map.Entry<Integer, Integer> freq : d.entrySet())
    {
         
        // If the element is not in
        // the dictionary, then store
        // the element in an array
        // with key as the frequency
        if (!occu.containsKey(freq.getValue()))
        {
            occu.put(freq.getValue(),
                     new ArrayList<Integer>());
            occu.get(freq.getValue()).add(
                     freq.getKey());
        }
 
        // Else, add the element to
        // the array of elements
        else
        {
            occu.get(freq.getValue()).add(
                     freq.getKey());
        }
    }
 
    // 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(Map.Entry<Integer,
        ArrayList<Integer>> a : occu.entrySet())
    {
        if (K == 0)
            return a.getValue();
 
        K -= 1;
    }
    return new ArrayList<Integer>();
}
 
// Driver code
public static void main(String[] args)
{
    ArrayList<Integer> arr = new ArrayList<Integer>(
        Arrays.asList(1, 4, 4, 4, 2, 2, 2, 5, 5,
                      5, 5, 5, 7, 7, 8, 8, 8, 8));
                       
    int K = 3;
    ArrayList<Integer> a = new ArrayList<Integer>(
        findKthMostOccurring(arr, K));
 
    System.out.print("[");
    for(int i = 0; i < a.size() - 1; i++)
    {
        System.out.print(a.get(i) + ", ");
    }
     
    if (a.size() >= 1)
        System.out.print((int)a.get(
            a.size() - 1) + "]");
}
}
 
// This code is contributed by akhilsaini
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

edit
close

play_arrow

link
brightness_4
code

// C# implementation to find K-th
// most occurring element in an array
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{
   
// Function to find K-th most
// occurring element in an array
static ArrayList findKthMostOccurring(ArrayList arr,
                                      int K)
{
     
    // Initializing a dictionary
    SortedDictionary<int,
                     int> d = new SortedDictionary<int,
                                                   int>();
  
    // Iterating through the array
    foreach(int i in arr)
    {
         
        // If the element is not in
        // the dictionary, adding it
        // with the frequency as 1
        if (!d.ContainsKey(i))
            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.Count < K)
        return new ArrayList();
  
    // Initializing a new dictionary
    // to store the elements according
    // to their frequency
    SortedDictionary<int,
                     ArrayList> occu = new SortedDictionary<int,
                                                            ArrayList>();
  
    // Iterating through the dictionary
    foreach(KeyValuePair<int, int> freq in d)
    {
         
        // If the element is not in
        // the dictionary, then store
        // the element in an array
        // with key as the frequency
        if (!occu.ContainsKey(freq.Value))
        {
            occu[freq.Value] = new ArrayList();
            occu[freq.Value].Add(freq.Key);
        }
         
        // Else, add the element to
        // the array of elements
        else
        {
            occu[freq.Value].Add(freq.Key);
        }
    }
     
    // Since the dictionary is sorted
    // and not indexed, find (M - K)-th
    // element where M is the length
    // of the dictionary
    K = occu.Count - K;
  
    // Since we for sure know that the
    // element exists, we iterate
    // through the dictionary and
    // return the element
    foreach(KeyValuePair<int, ArrayList> a in occu)
    {
        if (K == 0)
            return a.Value;
             
        K -= 1;
    }
    return new ArrayList();
 
// Driver code
public static void Main(string[] args)
{
    ArrayList arr = new ArrayList(){ 1, 4, 4, 4, 2, 2,
                                     2, 5, 5, 5, 5, 5,
                                     7, 7, 8, 8, 8, 8 };
    int K = 3;
    ArrayList a = findKthMostOccurring(arr, K);
     
    Console.Write("[");
    for(int i = 0; i < a.Count - 1; i++)
    {
        Console.Write(a[i] + ", ");
    }
    Console.Write((int)a[a.Count - 1] + "]");
}
}
 
// This code is contributed by rutvik_56
chevron_right

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.





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.



Article Tags :
Practice Tags :