📌  相关文章
📜  计算频率等于其值的元素

📅  最后修改于: 2021-10-28 01:41:20             🧑  作者: Mango

给定一个大小为N的整数arr[]数组,任务是计算数组中频率等于其值的所有元素。

例子:

方法:使用映射存储数组中每个元素的频率,最后计算所有频率等于它们的值的元素。

下面是上述方法的实现:

C++
// C++ program to count the elements
// having frequency equals to its value
 
#include 
using namespace std;
 
// Function to find the count
int find_maxm(int arr[], int n)
{
    // Hash map for counting frequency
    map mpp;
 
    for (int i = 0; i < n; i++) {
 
        // Counting freq of each element
        mpp[arr[i]] += 1;
    }
 
    int ans = 0;
    for (auto x : mpp) {
        int value = x.first;
        int freq = x.second;
 
        // Check if value equals to frequency
        // and increment the count
        if (value == freq) {
            ans++;
        }
    }
 
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 3, 2, 2, 3, 4, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    cout << find_maxm(arr, n);
 
    return 0;
}


Java
// Java program to count the elements
// having frequency equals to its value
import java.util.*;
 
class GFG{
  
// Function to find the count
static int find_maxm(int arr[], int n)
{
    // Hash map for counting frequency
    HashMap mp = new HashMap();
  
    for (int i = 0; i < n; i++) {
  
        // Counting freq of each element
        if(mp.containsKey(arr[i])){
            mp.put(arr[i], mp.get(arr[i])+1);
        }else{
            mp.put(arr[i], 1);
    }
    }
  
    int ans = 0;
    for (Map.Entry x : mp.entrySet()){
        int value = x.getKey();
        int freq = x.getValue();
  
        // Check if value equals to frequency
        // and increment the count
        if (value == freq) {
            ans++;
        }
    }
  
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 3, 2, 2, 3, 4, 3 };
    int n = arr.length;
  
    // Function call
    System.out.print(find_maxm(arr, n));
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 program to count the elements
# having frequency equals to its value
 
# Function to find the count
def find_maxm(arr, n):
     
    # Hash map for counting frequency
    mpp = {}
     
    for i in range (0, n):
         
        # Counting freq of each element
        if arr[i] in mpp:
            mpp[arr[i]] = mpp[arr[i]] + 1
        else:
            mpp[arr[i]] = 1
     
    ans = 0
     
    for key in mpp:
        value = key
        freq = mpp[key]
         
        # Check if value equals to frequency
        # and increment the count
        if value == freq:
            ans = ans + 1
     
    return ans
 
# Driver code
if __name__ == "__main__":
     
    arr = [ 3, 2, 2, 3, 4, 3 ]
    n = len(arr)
     
    # Function call
    print(find_maxm(arr, n))
   
# This code is contributed by akhilsaini


C#
// C# program to count the elements
// having frequency equals to its value
using System;
using System.Collections.Generic;
 
class GFG{
   
// Function to find the count
static int find_maxm(int []arr, int n)
{
    // Hash map for counting frequency
    Dictionary mp = new Dictionary();
   
    for (int i = 0; i < n; i++) {
   
        // Counting freq of each element
        if(mp.ContainsKey(arr[i])){
            mp[arr[i]] = mp[arr[i]] + 1;
        }else{
            mp.Add(arr[i], 1);
    }
    }
   
    int ans = 0;
    foreach (KeyValuePair x in mp){
        int value = x.Key;
        int freq = x.Value;
   
        // Check if value equals to frequency
        // and increment the count
        if (value == freq) {
            ans++;
        }
    }
   
    return ans;
}
   
// Driver code
public static void Main(String[] args)
{
    int []arr = { 3, 2, 2, 3, 4, 3 };
    int n = arr.Length;
   
    // Function call
    Console.Write(find_maxm(arr, n));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


Python3
# Python3 program to count the elements
# having frequency equals to its value
# importing counter from collections
from collections import Counter
 
# Function to find the count
def findElements(arr, n):
   
    # Now create dictionary using counter method
    # which will have elements as key and their
    # frequencies as values
    Element_Counter = Counter(arr)
    ans = 0
 
    for key in Element_Counter:
        value = key
        freq = Element_Counter[key]
 
        # Check if value equals to frequency
        # and increment the count
        if value == freq:
            ans = ans + 1
 
    return ans
 
 
# Driver code
arr = [3, 2, 2, 3, 4, 3]
n = len(arr)
 
# Function call
print(findElements(arr, n))
 
# This code is contributed by vikkycirus


输出:
2

方法#2:使用 collections.Counter()

我们可以使用Python Counter()方法快速解决这个问题。方法很简单。

  • 首先使用 Counter 方法创建一个字典,将元素作为键,将它们的频率作为值
  • 计算所有频率等于它们的值(键)的元素

下面是上述方法的实现:

蟒蛇3

# Python3 program to count the elements
# having frequency equals to its value
# importing counter from collections
from collections import Counter
 
# Function to find the count
def findElements(arr, n):
   
    # Now create dictionary using counter method
    # which will have elements as key and their
    # frequencies as values
    Element_Counter = Counter(arr)
    ans = 0
 
    for key in Element_Counter:
        value = key
        freq = Element_Counter[key]
 
        # Check if value equals to frequency
        # and increment the count
        if value == freq:
            ans = ans + 1
 
    return ans
 
 
# Driver code
arr = [3, 2, 2, 3, 4, 3]
n = len(arr)
 
# Function call
print(findElements(arr, n))
 
# This code is contributed by vikkycirus

输出:

2

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程