Find the Difference of Two Arrays in Python – LeetCode 75 Explained

The Find the Difference of Two Arrays problem from LeetCode (Problem 2215) is a beginner-friendly coding interview question that tests your understanding of sets, arrays and membership checks. It’s a common warm-up problem to evaluate how you handle uniqueness and comparison logic.

Find the Difference of Two Arrays in Python – LeetCode Explained

In this blog, we’ll break down the problem, walk through examples, and explain a clean Python solution step by step.

Problem Statement

You are given two integer arrays, nums1 and nums2.

Return a list of two lists:

  1. The first list should contain all distinct integers in nums1 that are not present in nums2.
  2. The second list should contain all distinct integers in nums2 that are not present in nums1.

Understanding the Problem

  • Both arrays may contain duplicates.
  • We only care about unique values, so we use sets.
  • The task is to find numbers that exist in one array but not in the other.
  • This is essentially a set difference problem.

Examples

Example 1:

Input:

nums1 = [1,2,3]
nums2 = [2,4,6]

Output:

[[1,3],[4,6]]

Explanation:

  • From nums1, numbers [1,3] are not in nums2.
  • From nums2, numbers [4,6] are not in nums1.

Example 2:

Input:

nums1 = [1,2,3,3]
nums2 = [1,1,2,2]

Output:

[[3], []]

Explanation:

  • nums1 has [3] that does not appear in nums2.
  • Every number in nums2 already exists in nums1.

Python Solution – Step by Step

Here’s the clean Python implementation:

from typing import List

class Solution:
    def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:
        # Step 1: Convert arrays to sets
        set_nums1 = set(nums1)
        set_nums2 = set(nums2)

        # Step 2: Find unique elements in each set
        distinct_nums1 = [num for num in set_nums1 if num not in set_nums2]
        distinct_nums2 = [num for num in set_nums2 if num not in set_nums1]

        # Step 3: Return the result
        return [distinct_nums1, distinct_nums2]

Step 1: Convert Arrays to Sets

set_nums1 = set(nums1)
set_nums2 = set(nums2)

This removes duplicates and allows fast membership checks.

Step 2: Find Unique Elements

distinct_nums1 = [num for num in set_nums1 if num not in set_nums2]
distinct_nums2 = [num for num in set_nums2 if num not in set_nums1]

We use list comprehensions to find elements unique to each set.

Step 3: Return the Result

return [distinct_nums1, distinct_nums2]

The output is returned as a list of two lists.

Why This Solution Works

  • Sets provide O(1) average lookup time, making comparisons efficient.
  • Using list comprehensions keeps the solution concise and Pythonic.
  • It handles duplicates automatically since sets only keep distinct values.

Time and Space Complexity

  • Time Complexity: O(n + m) → converting lists to sets and iterating.
  • Space Complexity: O(n + m) → storing unique values in sets.

Edge Cases to Consider

InputOutputExplanation
nums1 = [1,1,1], nums2 = [1,1][[], []]Both arrays contain the same numbers.
nums1 = [], nums2 = [4,5][[], [4,5]]If one array is empty, return the unique numbers of the other.
nums1 = [7,8], nums2 = [][[7,8], []]Symmetric case of above.

Real-World Applications

  • Database comparison: Finding records present in one dataset but not another.
  • File sync tools: Detecting files in one folder that are missing in another.
  • User analytics: Identifying unique users between two platforms.

Conclusion

The Find the Difference of Two Arrays problem is a great way to practice set operations in Python. By converting arrays into sets and comparing their contents, we can solve the problem efficiently in O(n + m) time.

This problem builds intuition for array difference, set operations, and data comparison—concepts that are widely used in interviews and real-world coding tasks.

3 thoughts on “Find the Difference of Two Arrays in Python – LeetCode 75 Explained”

Leave a Comment