Check if frequency of each element in given array is unique or not | Python3 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
from collections import defaultdict NEW_LINE def checkUniqueFrequency ( arr , n ) : NEW_LINE INDENT freq = defaultdict ( int ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ arr [ i ] ] += 1 NEW_LINE DEDENT uniqueFreq = set ( [ ] ) NEW_LINE for i in freq : NEW_LINE INDENT if ( freq [ i ] in uniqueFreq ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT uniqueFreq . add ( freq [ i ] ) NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 2 , 5 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE res = checkUniqueFrequency ( arr , n ) NEW_LINE if ( res ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT