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

Explanation :

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

Follow the steps below to solve the problem:

- Store the frequencies of every array element with value less than equal to array size in a Hashmap.
- The reason for above step is that the frequency of an element can be atmost equal to the size of the given array. Hence, there is no need to store frequency of an element, whose value is greater than given array size.
- Count the integers having frequencies equal to its value.
- Print the final count.

Below is the implementation of the above approach:

## C++

`#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the integer ` `// which has a frequency in the ` `// array equal to its value. ` `void` `solve(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Store frequency of array ` ` ` `// elements ` ` ` `map<` `int` `, ` `int` `> freq; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// Store the frequency ` ` ` `// only if arr[i]<=n ` ` ` `if` `(arr[i] <= n) ` ` ` `freq[arr[i]]++; ` ` ` `} ` ` ` ` ` `// Initially the count is zero ` ` ` `int` `count = 0; ` ` ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` `// If the freq[i] is equal ` ` ` `// to i, then increment ` ` ` `// the count by 1 ` ` ` `if` `(i == freq[i]) { ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print the final count ` ` ` `cout << count << ` `"\n"` `; ` `} ` ` ` `// Driver Program ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 3, 1, 1, 3, 2, 2, 3 }; ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `solve(arr, N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** O(N)

**Auxillary Space Complexity:** O(N)

Refer to the previous post for **O(N*log(N))** approach.

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
- Count minimum frequency elements in a linked list
- Maximum difference between frequency of two elements such that element having greater frequency is also greater
- Value to be subtracted from array elements to make sum of all elements equals K
- Count all Quadruples from four arrays such that their XOR equals to 'x'
- Sum of all even frequency elements in Matrix
- Sort elements by frequency | Set 2
- Bitwise XOR of elements having odd frequency
- Sum of all odd frequency elements in an array
- Sum of all odd frequency elements in a Matrix
- Sort elements by frequency | Set 1
- Count numbers whose maximum sum of distinct digit-sum is less than or equals M
- Delete all odd frequency elements from an Array
- Largest subarray with frequency of all elements same
- Sorting Array Elements By Frequency | Set 3 (Using STL)
- XOR of elements in an array having prime frequency
- Sum of elements in an array having prime frequency
- Sum of elements in an array having composite frequency
- Sort elements by frequency | Set 5 (using Java Map)
- Sum of all minimum frequency elements in Matrix

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.