easy +10 pts

Union of Many Sets

Flatten multiple sets into one unique collection and return a sorted list.

Write a function `union_of_sets(*sets)` that accepts zero or more set objects as positional arguments. The function should return a sorted list containing every distinct element that appears in any of the given sets. The order of the returned list must be ascending according to Python's natural ordering. If no sets are provided, return an empty list. The input sets may contain mixed types (e.g., integers, strings) only if all elements are mutually comparable; otherwise behavior is undefined, but the test cases will use homogeneous types.

Constraints

Each set contains up to 1000 elements. The total number of elements across all sets does not exceed 10,000. The number of sets passed is between 0 and 100. The solution should run in O(total elements) time and O(union size) space.

Example

>>> union_of_sets({1, 2}, {2, 3})
[1, 2, 3]
>>> union_of_sets({1}, {2}, {3})
[1, 2, 3]
>>> union_of_sets()
[]
>>> union_of_sets({'a', 'b'}, {'b', 'c'})
['a', 'b', 'c']
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

You can use the set union operator or method repeatedly over all sets.
Starting with an empty set and updating it with each set works well.
After collecting all unique elements, sorted() will produce the required list.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.