14. Problem to find the maximum element in a set

In this Python set program, we will find the maximum element in a set with the help of the below-given steps.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Maximum element in a set:

Method 1 : Using Logic

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. Create a variable maximum and assign its value equal to 0.
4. Use a for loop to iterate over elements in the set.
5. If the value of the element is greater than value of the maximum variable then assign that value to the maximum variable.
6. Print the output to get the maximum element in a set.

				
					Set = {10,23,45,66,96,83}
print("Original set1: ",Set)
maximum = 0
for ele in Set:
    if ele > maximum:
        maximum = ele
print("Maximum value: ",maximum)
				
			

Output :

				
					Original set1:  {96, 66, 10, 45, 83, 23}
Maximum value:  96
				
			

Method 2 : Using in-built function.

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. Get the maximum element in a set using max() function.

				
					Set = {10,23,45,66,96,83}
print("Original set1: ",Set)
print("Maximum value: ",max(Set))
				
			

Output :

				
					Original set1:  {96, 66, 10, 45, 83, 23}
Maximum value:  96
				
			

Related Articles

convert a set to a list.

find the minimum element in a set.

Leave a Comment