File: find-the-difference-of-two-arrays/solution.py

Date: 2026-06-06

Time: 16:44

find-the-difference-of-two-arrays/solution.py

Purpose

This file implements LeetCode 2215 — Find the Difference of Two Arrays. It owns the sole responsibility of computing the symmetric set difference between two integer arrays: elements unique to nums1 and elements unique to nums2, returned as two separate lists.

Key Components

Solution.findDifference(nums1, nums2) -> List[List[int]]

The only method. Contract:

Patterns

Set-difference idiom. The solution converts both arrays to sets, then uses Python's - operator for set difference. This is the canonical Python approach — it's both the most readable and the most efficient way to express this operation.

Single-expression body. The entire logic fits in two lines: one for conversion, one for the result. This is representative of a pattern across the repo where solutions favor concise, idiomatic Python over verbose imperative loops.

Dependencies

Imports: List from typing — used only for the type annotation matching LeetCode's method signature.

Imported by: The test_solution.py in the same directory. The massive "Imported By" list in the prompt is misleading — those are test files for *other* problems that happen to share the same Solution class name pattern, not actual imports of this file.

Flow

1. set(nums1) and set(nums2) — O(n + m) conversion that deduplicates both inputs.

2. set1 - set2 — O(min(len(set1), len(set2))) set difference: elements in set1 not present in set2.

3. set2 - set1 — same, reversed direction.

4. Both differences are wrapped with list() and returned as a two-element list.

Total time complexity: O(n + m). Space: O(n + m) for the two sets.

Invariants

Error Handling

None. The function trusts its inputs match the LeetCode contract (integer lists). No validation, no exceptions. This is appropriate — it's called only from the test harness and LeetCode's runtime, both of which guarantee valid inputs.

Topics to Explore

Beliefs