Minimum removals required to make frequency of all remaining array elements equal | Python 3 program for the above approach ; Function to count the minimum removals required to make frequency of all array elements equal ; Stores frequency of all array elements ; Traverse the array ; Stores all the frequencies ; Traverse the map ; Sort the frequencies ; Count of frequencies ; Stores the final count ; Traverse the vector ; Count the number of removals for each frequency and update the minimum removals required ; Print the final count ; Driver Code ; Given array ; Size of the array ; Function call to print the minimum number of removals required
from collections import defaultdict NEW_LINE def minDeletions ( 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 v = [ ] NEW_LINE for z in freq . keys ( ) : NEW_LINE INDENT v . append ( freq [ z ] ) NEW_LINE DEDENT v . sort ( ) NEW_LINE size = len ( v ) NEW_LINE ans = N - ( v [ 0 ] * size ) NEW_LINE for i in range ( 1 , len ( v ) ) : NEW_LINE INDENT if ( v [ i ] != v [ i - 1 ] ) : NEW_LINE INDENT safe = v [ i ] * ( size - i ) NEW_LINE ans = min ( ans , N - safe ) NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 4 , 3 , 2 , 5 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE minDeletions ( arr , N ) NEW_LINE DEDENT