Count the elements having frequency equals to its value

• Difficulty Level : Easy
• Last Updated : 09 Jul, 2021

Given an array of integers arr[] of size N, the task is to count all the elements of the array which have a frequency equals to its value.

Examples:

Input: arr[] = {3, 2, 2, 3, 4, 3}
Output:
Frequency of element 2 is 2
Frequency of element 3 is 3
Frequency of element 4 is 1
2 and 3 are elements which have same frequency as it’s value

Input: arr[] = {1, 2, 3, 4, 5, 6}
Output:

Approach: Store the frequency of every element of the array using the map, and finally count all of that elements whose frequency is equal to their value.

Below is the implementation of the above approach:

C++

 // C++ program to count the elements// having frequency equals to its value #include using namespace std; // Function to find the countint 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 codeint main(){    int arr[] = { 3, 2, 2, 3, 4, 3 };    int n = sizeof(arr) / sizeof(arr);     // Function call    cout << find_maxm(arr, n);     return 0;}

Java

 // Java program to count the elements// having frequency equals to its valueimport java.util.*; class GFG{  // Function to find the countstatic 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 codepublic 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 countdef 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 codeif __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 valueusing System;using System.Collections.Generic; class GFG{   // Function to find the countstatic 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 codepublic 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


Output:
2

Method #2:Using collections.Counter()

We can solve this problem quickly using python Counter() method. Approach is very simple.

• First create a dictionary using Counter method having elements as keys and their frequencies as values
• count all of that elements whose frequency is equal to their value(key)

Below is the implementation of above approach:

Python3

 # Python3 program to count the elements# having frequency equals to its value# importing counter from collectionsfrom collections import Counter # Function to find the countdef 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 codearr = [3, 2, 2, 3, 4, 3]n = len(arr) # Function callprint(findElements(arr, n)) # This code is contributed by vikkycirus

Output:

2

My Personal Notes arrow_drop_up