Find the minimum number of operations required to make all array elements equal | Python3 implementation of the approach ; Function to return the minimum operations required to make all array elements equal ; To store the frequency of all the array elements ; Traverse through array elements and update frequencies ; To store the maximum frequency of an element from the array ; Traverse through the map and find the maximum frequency for any element ; Return the minimum operations required ; Driver code
import sys NEW_LINE def minOperations ( arr , n ) : NEW_LINE INDENT mp = dict . fromkeys ( arr , 0 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 ; NEW_LINE DEDENT maxFreq = - ( sys . maxsize - 1 ) ; NEW_LINE for key in mp : NEW_LINE INDENT maxFreq = max ( maxFreq , mp [ key ] ) ; NEW_LINE DEDENT return ( n - maxFreq ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 4 , 6 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( minOperations ( arr , n ) ) ; NEW_LINE DEDENT