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

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 the value less than equal to the given array size in
**freq[]**array. - The reason for the above step is that the frequency of an element can be at most equal to the size of the given array. Hence, there is no need to store the frequency of an element, whose value is greater than the 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++

`// C++ program of the ` `// above approach` `#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` ` ` `int` `freq[n+1] ={0};` ` ` `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 Code` `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*

## Java

`// Java program of the ` `// above approach` `class` `GFG{` `// Function to find the integer` `// which has a frequency in the` `// array equal to its value.` `static` `void` `solve(` `int` `arr[], ` ` ` `int` `n)` `{` ` ` `// Store frequency of array` ` ` `// elements` ` ` `int` `[]freq = ` `new` `int` `[n + ` `1` `];` ` ` `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` ` ` `System.out.print(count + ` `"\n"` `);` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `arr[] = {` `3` `, ` `1` `, ` `1` `, ` `3` `, ` `2` `, ` `2` `, ` `3` `};` ` ` `int` `N = arr.length;` ` ` `solve(arr, N);` `}` `}` `// This code is contributed by shikhasingrajput` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program of the` `# above approach` `# Function to find the integer` `# which has a frequency in the` `# array equal to its value.` `def` `solve(arr, n):` ` ` ` ` `# Store frequency of array` ` ` `# elements` ` ` `freq ` `=` `[` `0` `] ` `*` `(n ` `+` `1` `);` ` ` `for` `i ` `in` `range` `(n):` ` ` ` ` `# Store the frequency` ` ` `# only if arr[i]<=n` ` ` `if` `(arr[i] <` `=` `n):` ` ` `freq[arr[i]] ` `+` `=` `1` `;` ` ` `# Initially the count is zero` ` ` `count ` `=` `0` `;` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `):` ` ` ` ` `# If the freq[i] is equal` ` ` `# to i, then increment` ` ` `# the count by 1` ` ` `if` `(i ` `=` `=` `freq[i]):` ` ` `count ` `+` `=` `1` `;` ` ` `# Print final count` ` ` `print` `(count , "");` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `arr ` `=` `[` `3` `, ` `1` `, ` `1` `, ` `3` `, ` ` ` `2` `, ` `2` `, ` `3` `];` ` ` `N ` `=` `len` `(arr);` ` ` `solve(arr, N);` `# This code is contributed by shikhasingrajput` |

*chevron_right*

*filter_none*

## C#

`// C# program of the ` `// above approach` `using` `System;` `class` `GFG{` `// Function to find the integer` `// which has a frequency in the` `// array equal to its value.` `static` `void` `solve(` `int` `[]arr, ` ` ` `int` `n)` `{` ` ` `// Store frequency of array` ` ` `// elements` ` ` `int` `[]freq = ` `new` `int` `[n + 1];` ` ` `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 readonly count` ` ` `Console.Write(count + ` `"\n"` `);` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `int` `[]arr = {3, 1, 1, 3, 2, 2, 3};` ` ` `int` `N = arr.Length;` ` ` `solve(arr, N);` `}` `}` `// This code is contributed by shikhasingrajput` |

*chevron_right*

*filter_none*

**Output**

2

**Time Complexity:** O(N) **Auxiliary Space:** 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.