Difference between highest and least frequencies in an array | Python code to find the difference between highest and least frequencies ; Put all elements in a hash map ; Find counts of maximum and minimum frequent elements ; Driver code
from collections import defaultdict NEW_LINE def findDiff ( arr , n ) : NEW_LINE INDENT mp = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT max_count = 0 ; min_count = n NEW_LINE for key , values in mp . items ( ) : NEW_LINE INDENT max_count = max ( max_count , values ) NEW_LINE min_count = min ( min_count , values ) NEW_LINE DEDENT return max_count - min_count NEW_LINE DEDENT arr = [ 7 , 8 , 4 , 5 , 4 , 1 , 1 , 7 , 7 , 2 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findDiff ( arr , n ) ) NEW_LINE