#C14449. Unique Elements between Two Lists
Unique Elements between Two Lists
Unique Elements between Two Lists
Given two lists of integers, your task is to identify the elements that are unique to each list. In other words, you need to find the integers that appear in one list but not in both. The output should be the collection of unique elements, sorted in increasing order, with no duplicates.
Key Idea: It is efficient to convert the lists into sets and then calculate the difference between the sets. The union of these differences yields the final unique elements. The expected average time complexity is O(n) where n is the combined number of elements in both lists.
Note: The order of the unique elements in the output must be in ascending order.
inputFormat
The input consists of four lines:
- An integer n1 representing the number of elements in the first list.
- n1 space-separated integers representing the first list.
- An integer n2 representing the number of elements in the second list.
- n2 space-separated integers representing the second list.
outputFormat
Output the unique elements from both lists as space-separated integers in ascending order. If there are no unique elements, output an empty line.## sample
7
1 2 3 4 4 5 6
6
4 5 6 7 8 9
1 2 3 7 8 9