Count the elements having frequency equals to its value | Set 2
Last Updated :
13 Sep, 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: 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 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++
#include <bits/stdc++.h>
using namespace std;
void solve( int arr[], int n)
{
int freq[n+1] ={0};
for ( int i = 0; i < n; i++) {
if (arr[i] <= n)
freq[arr[i]]++;
}
int count = 0;
for ( int i = 1; i <= n; i++) {
if (i == freq[i]) {
count++;
}
}
cout << count << "\n" ;
}
int main()
{
int arr[] = { 3, 1, 1, 3, 2, 2, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
solve(arr, N);
return 0;
}
|
Java
class GFG{
static void solve( int arr[],
int n)
{
int []freq = new int [n + 1 ];
for ( int i = 0 ; i < n; i++)
{
if (arr[i] <= n)
freq[arr[i]]++;
}
int count = 0 ;
for ( int i = 1 ; i <= n; i++)
{
if (i == freq[i])
{
count++;
}
}
System.out.print(count + "\n" );
}
public static void main(String[] args)
{
int arr[] = { 3 , 1 , 1 , 3 , 2 , 2 , 3 };
int N = arr.length;
solve(arr, N);
}
}
|
Python3
def solve(arr, n):
freq = [ 0 ] * (n + 1 );
for i in range (n):
if (arr[i] < = n):
freq[arr[i]] + = 1 ;
count = 0 ;
for i in range ( 1 , n + 1 ):
if (i = = freq[i]):
count + = 1 ;
print (count , "");
if __name__ = = '__main__' :
arr = [ 3 , 1 , 1 , 3 ,
2 , 2 , 3 ];
N = len (arr);
solve(arr, N);
|
C#
using System;
class GFG{
static void solve( int []arr,
int n)
{
int []freq = new int [n + 1];
for ( int i = 0; i < n; i++)
{
if (arr[i] <= n)
freq[arr[i]]++;
}
int count = 0;
for ( int i = 1; i <= n; i++)
{
if (i == freq[i])
{
count++;
}
}
Console.Write(count + "\n" );
}
public static void Main(String[] args)
{
int []arr = {3, 1, 1, 3, 2, 2, 3};
int N = arr.Length;
solve(arr, N);
}
}
|
Javascript
<script>
function solve(arr, n)
{
let freq = Array.from({length: n+1}, (_, i) => 0);
for (let i = 0; i < n; i++)
{
if (arr[i] <= n)
freq[arr[i]]++;
}
let count = 0;
for (let i = 1; i <= n; i++)
{
if (i == freq[i])
{
count++;
}
}
document.write(count + "<br/>" );
}
let arr = [ 3, 1, 1, 3, 2, 2, 3 ];
let N = arr.length;
solve(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Refer to the previous post for O(N*log(N)) approach.
Share your thoughts in the comments
Please Login to comment...