easy +8 pts

Symmetric Difference

Return elements that appear in exactly one of two collections.

Write a function `symmetric_difference(a, b)` that takes two lists `a` and `b` and returns a sorted list of elements that appear in exactly one of the two lists. Duplicate elements within each list should be treated as a single occurrence. The result must be sorted in ascending numeric order.

Constraints

The input lists can contain any integers. The lists may be empty or contain duplicates. The time complexity should be O(n log n) where n is the total number of unique elements.

Example

>>> symmetric_difference([1, 2, 3], [2, 3, 4])
[1, 4]
>>> symmetric_difference([1, 1, 2], [2, 3])
[1, 3]
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Convert each list to a set to remove duplicates.
Use the set symmetric difference operator `^` or the method `.symmetric_difference()`.
Convert the result to a list and sort it before returning.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.