给定一个数组arr[] ,任务是找到给定数组元素的频率模式。
例子:
Input: arr[] = {6, 10, 3, 10, 8, 3, 6, 4}, N = 8
Output: 2
Explanation:
Here (3, 10 and 6) have frequency 2, while (4 and 8) have frequency 1.
Three numbers have frequency 2, while 2 numbers have frequency 1.
Thus, the mode of the frequencies is 2.
Input: arr[] = {5, 9, 2, 9, 7, 2, 5, 3, 1}, N = 9
Output:1
方法:思路是求所有数组元素的频率的频率。最后,计算频率的模式。
下面是上述方法的实现:
C++
// C++ program of the
// above approach
#include
using namespace std;
// Function to find the mode of the
// frequency of the array
int countFreq(int arr[], int n)
{
// Stores the frequencies
// of array elements
unordered_map mp1;
// Traverse through array
// elements and
// count frequencies
for (int i = 0; i < n; ++i) {
mp1[arr[i]]++;
}
// Stores the frequencies's of
// frequencies of array elements
unordered_map mp2;
for (auto it : mp1) {
mp2[it.second]++;
}
// Stores teh minimum value
int M = INT_MIN;
// Find the Mode in 2nd map
for (auto it : mp2) {
M = max(M, it.second);
}
// search for this Mode
for (auto it : mp2) {
// When mode is find then return
// to main function.
if (M == it.second) {
return it.first;
}
}
// If mode is not found
return 0;
}
// Driver Code
int main()
{
int arr[] = { 6, 10, 3, 10, 8, 3, 6, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << countFreq(arr, n);
return 0;
}
Java
// Java program of the
// above approach
import java.util.*;
class GFG{
// Function to find the mode of the
// frequency of the array
static int countFreq(int arr[], int n)
{
// Stores the frequencies
// of array elements
HashMap mp1 = new HashMap();
// Traverse through array
// elements and
// count frequencies
for(int i = 0; i < n; ++i)
{
if (mp1.containsKey(arr[i]))
{
mp1.put(arr[i], mp1.get(arr[i]) + 1);
}
else
{
mp1.put(arr[i], 1);
}
}
// Stores the frequencies's of
// frequencies of array elements
HashMap mp2 = new HashMap();
for(Map.Entry it : mp1.entrySet())
{
if (mp2.containsKey(it.getValue()))
{
mp2.put(it.getValue(),
mp2.get(it.getValue()) + 1);
}
else
{
mp2.put(it.getValue(), 1);
}
}
// Stores teh minimum value
int M = Integer.MIN_VALUE;
// Find the Mode in 2nd map
for(Map.Entry it : mp2.entrySet())
{
M = Math.max(M, it.getValue());
}
// Search for this Mode
for(Map.Entry it : mp2.entrySet())
{
// When mode is find then return
// to main function.
if (M == it.getValue())
{
return it.getKey();
}
}
// If mode is not found
return 0;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 6, 10, 3, 10, 8, 3, 6, 4 };
int n = arr.length;
System.out.print(countFreq(arr, n));
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program of the
# above approach
from collections import defaultdict
import sys
# Function to find the mode of the
# frequency of the array
def countFreq(arr, n):
# Stores the frequencies
# of array elements
mp1 = defaultdict (int)
# Traverse through array
# elements and
# count frequencies
for i in range (n):
mp1[arr[i]] += 1
# Stores the frequencies's of
# frequencies of array elements
mp2 = defaultdict (int)
for it in mp1:
mp2[mp1[it]] += 1
# Stores teh minimum value
M = -sys.maxsize - 1
# Find the Mode in 2nd map
for it in mp2:
M = max(M, mp2[it])
# search for this Mode
for it in mp2:
# When mode is find then return
# to main function.
if (M == mp2[it]):
return it
# If mode is not found
return 0
# Driver Code
if __name__ == "__main__":
arr = [6, 10, 3, 10,
8, 3, 6, 4]
n = len(arr)
print (countFreq(arr, n))
# This code is contributed by Chitranayal
C#
// C# program of the
// above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the mode of the
// frequency of the array
static int countFreq(int []arr, int n)
{
// Stores the frequencies
// of array elements
Dictionary mp1 = new Dictionary();
// Traverse through array
// elements and
// count frequencies
for(int i = 0; i < n; ++i)
{
if (mp1.ContainsKey(arr[i]))
{
mp1[arr[i]] = mp1[arr[i]] + 1;
}
else
{
mp1.Add(arr[i], 1);
}
}
// Stores the frequencies's of
// frequencies of array elements
Dictionary mp2 = new Dictionary();
foreach(KeyValuePair it in mp1)
{
if (mp2.ContainsKey(it.Value))
{
mp2[it.Value] =
mp2[it.Value] + 1;
}
else
{
mp2.Add(it.Value, 1);
}
}
// Stores teh minimum value
int M = int.MinValue;
// Find the Mode in 2nd map
foreach(KeyValuePair it in mp2)
{
M = Math.Max(M, it.Value);
}
// Search for this Mode
foreach(KeyValuePair it in mp2)
{
// When mode is find then return
// to main function.
if (M == it.Value)
{
return it.Key;
}
}
// If mode is not found
return 0;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 6, 10, 3, 10, 8, 3, 6, 4 };
int n = arr.Length;
Console.Write(countFreq(arr, n));
}
}
// This code is contributed by Amit Katiyar
Javascript
输出:
2
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。