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:2

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:1

**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 <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the count ` `int` `find_maxm(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Hash map for counting frquency ` ` ` `map<` `int` `, ` `int` `> 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 equls 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; ` `} ` |

*chevron_right*

*filter_none*

## 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 frquency ` ` ` `HashMap<Integer,Integer> mp = ` `new` `HashMap<Integer,Integer>(); ` ` ` ` ` `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<Integer,Integer> x : mp.entrySet()){ ` ` ` `int` `value = x.getKey(); ` ` ` `int` `freq = x.getValue(); ` ` ` ` ` `// Check if value equls 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 ` |

*chevron_right*

*filter_none*

## 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 frquency ` ` ` `Dictionary<` `int` `,` `int` `> mp = ` `new` `Dictionary<` `int` `,` `int` `>(); ` ` ` ` ` `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<` `int` `,` `int` `> x ` `in` `mp){ ` ` ` `int` `value = x.Key; ` ` ` `int` `freq = x.Value; ` ` ` ` ` `// Check if value equls 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 ` |

*chevron_right*

*filter_none*

**Output:**

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count the elements having frequency equals to its value | Set 2
- Maximum difference between frequency of two elements such that element having greater frequency is also greater
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Value to be subtracted from array elements to make sum of all elements equals K
- Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's
- Largest number in the Array having frequency same as value
- Replace every elements in the array by its frequency in the array
- Sum of elements in an array having prime frequency
- Find the element having different frequency than other array elements
- Product of elements in an array having prime frequency
- XOR of elements in an array having prime frequency
- Sum of elements in an array having composite frequency
- Smallest subarray having an element with frequency greater than that of other elements
- Bitwise XOR of elements having odd frequency
- Queries to count characters having odd frequency in a range [L, R]
- Count of Substrings with at least K pairwise Distinct Characters having same Frequency
- Count of elements having Euler's Totient value one less than itself
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.