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:
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++ 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 |
// 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 |
# 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))
|
// 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 |
<script> // Javascript implementation to find K-th // most occurring element in an array // Function to find K-th most // occurring element in an array function findKthMostOccurring(arr,K)
{ // Initializing a dictionary
let d = new Map();
// Iterating through the array
for (let i = 0; i < arr.length; i++)
{
// If the element is not in
// the dictionary, adding it
// with the frequency as 1
if (!d.has(arr[i]))
d.set(arr[i], 1);
// If the element is already
// present in the dictionary,
// increment its frequency
else
{
let temp = d.get(arr[i]);
temp += 1;
d.set(arr[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.length < K)
return [];
// Initializing a new dictionary
// to store the elements according
// to their frequency
let occu = new Map();
// Iterating through the dictionary
for (let [key, value] of d.entries())
{
// If the element is not in
// the dictionary, then store
// the element in an array
// with key as the frequency
if (!occu.has(value))
{
occu.set(value,[key]);
}
// Else, add the element to
// the array of elements
else
{
occu.get(value).push(key);
}
}
// 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 (let [key, value] of occu.entries())
{
if (K == 0)
return value;
K -= 1;
}
return [];
} // Driver code let arr=[1, 4, 4, 4, 2, 2, 2, 5, 5, 5, 5, 5, 7, 7, 8, 8, 8, 8];
let K = 3; let a=findKthMostOccurring(arr, K); document.write( "[" +a.join( ", " )+ "]" );
// This code is contributed by patel2127 </script> |
[2, 4]
Time Complexity: O(NlogN), where N is the size of the given array.
Auxiliary Space: O(N), where N is the size of the given array.