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
def MAX(sets):
return (max(sets))
sets = set([8, 16, 24, 1, 25, 3, 10, 65, 55])
print(MAX(sets))
|
Output:
65
min() in a Set
Python3
def MIN(sets):
return (min(sets))
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
24 Feb, 2023
Like Article
Save Article