 # Python Program to Minimum Operations to make all Elements of the List Equal

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Given a list, the task is to find the minimum operations needed to make all the elements of the given list equal in Python

Examples:

Example1:

Input:

Given list = [11, 3, 7, 1, 2, 7, 3, 6, 7, 9, 3, 9, 1, 19]

Output:

The minimum operations to make all the elements of the given list [11, 3, 7, 1, 2, 7, 3, 6, 7, 9, 3, 9, 1, 19] equal is:
[ 11 ]

Example2:

Input:

Given list = [9, 9, 8, 9, 5, 8, 3, 1, 4, 7]

Output:

The minimum operations to make all the elements of the given list [9, 9, 8, 9, 5, 8, 3, 1, 4, 7] equal is:
[ 7 ]

## Program to Minimum Operation to make all Elements of the List Equal in Python

Below are the ways to find the minimum operations needed to make all the elements of the given list equal in Python

### Method #1: Using Counter() (Hashing , Static Input)

Approach:

• Import the Counter() function from collections using the import keyword.
• Give the list as static input and store it in a variable.
• Calculate the frequency of all the given list elements using the Counter() function which returns the element and its frequency as key-value pair and store this dictionary in a variable(say freqyValues)
• Calculate the max value of the freqyValues using values() and max() function and store it in a variable(say mxfrqncyval)
• Calculate the length of the given list using the len() function and store it in a variable.
• Subtract the mxfrqncyval from the length of the given list.
• Print the result.
• The Exit of the Program.

Below is the implementation:

# Import the Counter() function from collections using the import keyword.
from collections import Counter
# Give the list as static input and store it in a variable.
givnlst = [11, 3, 7, 1, 2, 7, 3, 6, 7, 9, 3, 9, 1, 19]
# Calculate the frequency of all the given list
# elements using the Counter() function which returns
# the element and its frequency as key-value pair and
# store this dictionary in a variable(say freqyValues)
freqncyValues = Counter(givnlst)
# Calculate the max value of the freqyValues using values()
# and max() function and store it in a variable(say mxfrqncyval)
mxfrqncyval = max(freqncyValues.values())
# Calculate the length of the given list using the len() function
# and store it in a variable.
lstlengt = len(givnlst)
# Subtract the mxfrqncyval from the length of the given list.
minresul = lstlengt-mxfrqncyval
print('The minimum operations to make all the elements of the given list',
givnlst, 'equal is:')
print('[', minresul, ']')


Output:

The minimum operations to make all the elements of the given list [11, 3, 7, 1, 2, 7, 3, 6, 7, 9, 3, 9, 1, 19] equal is:
[ 11 ]

### Method #2: Using Counter() (Hashing , User Input)

Approach:

• Import the Counter() function from collections using the import keyword.
• Give the list as user input using list(),map(),input(),and split() functions.
• Store it in a variable.
• Calculate the frequency of all the given list elements using the Counter() function which returns the element and its frequency as key-value pair and store this dictionary in a variable(say freqyValues)
• Calculate the max value of the freqyValues using values() and max() function and store it in a variable(say mxfrqncyval)
• Calculate the length of the given list using the len() function and store it in a variable.
• Subtract the mxfrqncyval from the length of the given list.
• Print the result.
• The Exit of the Program.

Below is the implementation:

# Import the Counter() function from collections using the import keyword.
from collections import Counter
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
givnlst = list(map(int, input(
'Enter some random List Elements separated by spaces = ').split()))
# Calculate the frequency of all the given list
# elements using the Counter() function which returns
# the element and its frequency as key-value pair and
# store this dictionary in a variable(say freqyValues)
freqncyValues = Counter(givnlst)
# Calculate the max value of the freqyValues using values()
# and max() function and store it in a variable(say mxfrqncyval)
mxfrqncyval = max(freqncyValues.values())
# Calculate the length of the given list using the len() function
# and store it in a variable.
lstlengt = len(givnlst)
# Subtract the mxfrqncyval from the length of the given list.
minresul = lstlengt-mxfrqncyval
print('The minimum operations to make all the elements of the given list',
givnlst, 'equal is:')
print('[', minresul, ']')


Output:

Enter some random List Elements separated by spaces = 9 9 8 9 5 8 3 1 4 7
The minimum operations to make all the elements of the given list [9, 9, 8, 9, 5, 8, 3, 1, 4, 7] equal is:
[ 7 ]

Related Programs: