Wednesday, October 8, 2025
HomeLanguagesPython | Maximum and Minimum in a Set

Python | Maximum and Minimum in a Set

In this article, we will learn how to get the maximum and minimum element in a set in Python, using the built-in functions of Python. Examples:

Input : set = ([8, 16, 24, 1, 25, 3, 10, 65, 55])
Output : max is 65

Input : set = ([4, 12, 10, 9, 4, 13])
Output : min is 4

max() in a Set

The built-in function max() in Python is used to get the maximum of all the elements in a set. 

Python3




# Python code to get the maximum element from a set
def MAX(sets):
    return (max(sets))
     
# Driver Code
sets = set([8, 16, 24, 1, 25, 3, 10, 65, 55])
print(MAX(sets))


Output:

65

min() in a Set

Python3




# Python code to get the minimum element from a set
def MIN(sets):
    return (min(sets))
     
# Driver Code
sets = set([4, 12, 10, 9, 4, 13])
print(MIN(sets))


Output:

4

Time complexity: O(n)
Auxiliary space: O(n), n is number of elements in set.

RELATED ARTICLES

Most Popular

Dominic
32341 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6710 POSTS0 COMMENTS
Nicole Veronica
11875 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6832 POSTS0 COMMENTS
Ted Musemwa
7092 POSTS0 COMMENTS
Thapelo Manthata
6786 POSTS0 COMMENTS
Umr Jansen
6788 POSTS0 COMMENTS