Common and Unique Elements Between Two Lists

Medium ⏱ 12 min 64% acceptance ★★★★★ 4.9
Write compare_lists_elements(a, b) that returns a tuple (common, only_in_a, only_in_b). common holds values present in both lists (no duplicates, any order), only_in_a holds values present in a but not b, and only_in_b the reverse. Return each as a sorted list for a deterministic result.

Examples

Example 1
Input
a = [1, 2, 3, 4], b = [3, 4, 5]
Output
([3, 4], [1, 2], [5])
Explanation

3 and 4 appear in both; 1, 2 only in a; 5 only in b.

Constraints

  • 0 <= len(a), len(b) <= 10^4

Topics

ListsComparison

Companies

AmazonAirbnb

Hints

Hint 1

Converting both lists to sets makes intersection/difference easy, then sort the results back into lists.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.