Count the elements having frequency equals to its value | C ++ program to count the elements having frequency equals to its value ; Function to find the count ; Hash map for counting frequency ; Counting freq of each element ; Check if value equals to frequency and increment the count ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find_maxm ( int arr [ ] , int n ) { map < int , int > mpp ; for ( int i = 0 ; i < n ; i ++ ) { mpp [ arr [ i ] ] += 1 ; } int ans = 0 ; for ( auto x : mpp ) { int value = x . first ; int freq = x . second ; if ( value == freq ) { ans ++ ; } } return ans ; } int main ( ) { int arr [ ] = { 3 , 2 , 2 , 3 , 4 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << find_maxm ( arr , n ) ; return 0 ; }