SDE & MLE Prep

2215. Find the Difference of Two Arrays

Problem

Return two lists: the distinct values of nums1 that aren't in nums2, and the distinct values of nums2 that aren't in nums1. Order within each list doesn't matter.

Example: nums1 = [1,2,3], nums2 = [2,4,6] → [[1,3],[4,6]]

Set difference both ways: set1 − set2 and set2 − set1 (duplicates collapse automatically). Either list may be returned in any order.

Complexity: O(n + m).

class Solution:
    def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:
        a, b = set(nums1), set(nums2)
        return [list(a - b), list(b - a)]