给定一个大小为N的整数arr[]数组和一个数字K ,任务是找到该数组中出现次数最多的第K个元素。
注意:如果数组中有多个数字具有相同的频率,则认为两者处于同一出现级别。因此打印两个数字。
例子:
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:
方法:想法是使用两个字典数据结构来存储元素的所有频率。
- 迭代给定的数组。
- 找到所有元素的频率并将其存储在字典中,键是数字,值是它的频率。
- 初始化另一个字典以将键存储为频率,将值存储为具有该频率的所有元素。
- 最后,由于字典已排序,找到字典中第(M – K)个位置的数组,其中 M 是数组中唯一元素的数量。
下面是上述方法的实现:
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 findKthMostOccurring(vector arr, int K){
// Initializing a dictionary
map 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 > 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()
{
vectorarr = {1, 4, 4, 4, 2, 2, 2, 5, 5,
5, 5, 5, 7, 7, 8, 8, 8, 8};
int K = 3;
vector 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
// 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 ArrayListfindKthMostOccurring(
ArrayList arr, int K)
{
// Initializing a dictionary
HashMap 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();
// Initializing a new dictionary
// to store the elements according
// to their frequency
HashMap> occu = new HashMap>();
// Iterating through the dictionary
for(Map.Entry 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());
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> a : occu.entrySet())
{
if (K == 0)
return a.getValue();
K -= 1;
}
return new ArrayList();
}
// Driver code
public static void main(String[] args)
{
ArrayList arr = new ArrayList(
Arrays.asList(1, 4, 4, 4, 2, 2, 2, 5, 5,
5, 5, 5, 7, 7, 8, 8, 8, 8));
int K = 3;
ArrayList a = new ArrayList(
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
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))
C#
// 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 d = new SortedDictionary();
// 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 occu = new SortedDictionary();
// Iterating through the dictionary
foreach(KeyValuePair 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 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
Javascript
输出:
[2, 4]
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。