Skip to main content

Find the Difference of Two Arrays - Video Solutions

EasyArrayHash Table

Find the Difference of Two Arrays - Leetcode 2215 - Python

NeetCodeIO
7:3418,873 views
10 video solutions available

Find the Difference of Two Arrays - Video Solution

Watch 10 video solutions for Find the Difference of Two Arrays, a easy level problem involving Array, Hash Table. This walkthrough by NeetCodeIO has 18,873 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where:

  • answer[0] is a list of all distinct integers in nums1 which are not present in nums2.
  • answer[1] is a list of all distinct integers in nums2 which are not present in nums1.

Note that the integers in the lists may be returned in any order.

 

Example 1:

Input: nums1 = [1,2,3], nums2 = [2,4,6]
Output: [[1,3],[4,6]]
Explanation:
For nums1, nums1[1] = 2 is present at index 0 of nums2, whereas nums1[0] = 1 and nums1[2] = 3 are not present in nums2. Therefore, answer[0] = [1,3].
For nums2, nums2[0] = 2 is present at index 1 of nums1, whereas nums2[1] = 4 and nums2[2] = 6 are not present in nums2. Therefore, answer[1] = [4,6].

Example 2:

Input: nums1 = [1,2,3,3], nums2 = [1,1,2,2]
Output: [[3],[]]
Explanation:
For nums1, nums1[2] and nums1[3] are not present in nums2. Since nums1[2] == nums1[3], their value is only included once and answer[0] = [3].
Every integer in nums2 is present in nums1. Therefore, answer[1] = [].

 

Constraints:

  • 1 <= nums1.length, nums2.length <= 1000
  • -1000 <= nums1[i], nums2[i] <= 1000
Read full problem with examples

Approach Overview

Problem Overview: You are given two integer arrays nums1 and nums2. The task is to return two lists: elements that appear in nums1 but not in nums2, and elements that appear in nums2 but not in nums1. Each result must contain distinct values only.

This is essentially a set-difference problem. You compare the unique elements of both arrays and determine which values exist exclusively in one array. The challenge is avoiding duplicates while keeping the algorithm efficient.

Approach 1: Using Sorting to Solve the Problem (O(n log n) time, O(1) extra space)

This approach sorts both arrays first, then scans them to identify elements that appear in only one array. After sorting, you iterate through both arrays with two pointers. When values match, move both pointers. When one value is smaller, it means that value does not appear in the other array at that position, so it belongs in the result. Duplicate values are skipped during traversal to ensure the output only contains unique elements.

The main advantage is minimal extra memory usage since sorting allows comparison without additional hash structures. The tradeoff is the O(n log n) sorting cost. This approach is useful when memory is constrained or when the arrays are already sorted. Sorting-based comparisons are common patterns in array problems where duplicates must be handled carefully.

Approach 2: Using a HashMap for Efficient Lookup (O(n + m) time, O(n + m) space)

The optimal solution converts both arrays into hash sets. A set automatically removes duplicates and provides constant-time membership checks. First insert all elements from nums1 into a set and all elements from nums2 into another set. Then iterate through the first set and add elements not found in the second set to the result list. Repeat the same process in reverse for the second set.

The key insight is that hash lookups run in average O(1) time, so the comparison becomes linear overall. Building the sets costs O(n + m), and the difference checks also run in linear time. This approach is a classic application of hash tables for fast membership testing and is commonly used in problems involving deduplication and set operations on arrays.

Recommended for interviews: The hash-set approach is what interviewers typically expect. It demonstrates that you recognize the problem as a set-difference operation and can use constant-time lookups to reduce complexity to O(n + m). Discussing the sorting approach first can show baseline reasoning, but the hash-based solution signals stronger familiarity with common array + hash table optimization patterns.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Sorting + Two PointersO(n log n + m log m)O(1) extra (excluding output)When memory is limited or arrays are already sorted
Hash Set / HashMap LookupO(n + m)O(n + m)General case and optimal interview solution with fast lookups