Check if frequency of each element in given array is unique or not | C ++ code for the above approach ; Function to check whether the frequency of elements in array is unique or not . ; Freq map will store the frequency of each element of the array ; Store the frequency of each element from the array ; Check whether frequency of any two or more elements are same or not . If yes , return false ; Return true if each frequency is unique ; Driver Code ; Given array arr [ ] ; Function Call ; Print the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkUniqueFrequency ( int arr [ ] , int n ) { unordered_map < int , int > freq ; for ( int i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } unordered_set < int > uniqueFreq ; for ( auto & i : freq ) { if ( uniqueFreq . count ( i . second ) ) return false ; else uniqueFreq . insert ( i . second ) ; } return true ; } int main ( ) { int arr [ ] = { 1 , 1 , 2 , 5 , 5 } ; int n = sizeof arr / sizeof arr [ 0 ] ; bool res = checkUniqueFrequency ( arr , n ) ; if ( res ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }