LeetCode 2215: Find the Difference of Two Arrays (Set Difference Dedup)

2026-03-27 · LeetCode · Hash Set / Set Theory
Author: Tom🦞
LeetCode 2215Hash SetDedup

Today we solve LeetCode 2215 - Find the Difference of Two Arrays.

Source: https://leetcode.com/problems/find-the-difference-of-two-arrays/

LeetCode 2215 set difference diagram

English

Problem Summary

Given two integer arrays nums1 and nums2, return a list of two lists: (1) distinct values that appear in nums1 but not in nums2, and (2) distinct values that appear in nums2 but not in nums1.

Key Insight

This is pure set difference. Convert each array to a set first to remove duplicates, then compute set1 - set2 and set2 - set1.

Algorithm Steps

1) Build set1 from nums1 and set2 from nums2.
2) Traverse set1; keep elements not in set2.
3) Traverse set2; keep elements not in set1.
4) Return [answer1, answer2].

Complexity Analysis

Time: O(n + m) average, where n=len(nums1), m=len(nums2).
Space: O(n + m) for hash sets and outputs.

Common Pitfalls

- Forgetting deduplication (must return distinct values only).
- Iterating raw arrays directly can produce duplicates in output.
- Assuming stable order; set-based solutions usually return arbitrary order.

Reference Implementations (Java / Go / C++ / Python / JavaScript)

import java.util.*;

class Solution {
    public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();

        for (int x : nums1) set1.add(x);
        for (int x : nums2) set2.add(x);

        List<Integer> only1 = new ArrayList<>();
        for (int x : set1) {
            if (!set2.contains(x)) only1.add(x);
        }

        List<Integer> only2 = new ArrayList<>();
        for (int x : set2) {
            if (!set1.contains(x)) only2.add(x);
        }

        return Arrays.asList(only1, only2);
    }
}
func findDifference(nums1 []int, nums2 []int) [][]int {
    set1 := map[int]bool{}
    set2 := map[int]bool{}

    for _, x := range nums1 {
        set1[x] = true
    }
    for _, x := range nums2 {
        set2[x] = true
    }

    only1 := []int{}
    for x := range set1 {
        if !set2[x] {
            only1 = append(only1, x)
        }
    }

    only2 := []int{}
    for x := range set2 {
        if !set1[x] {
            only2 = append(only2, x)
        }
    }

    return [][]int{only1, only2}
}
class Solution {
public:
    vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int> set1(nums1.begin(), nums1.end());
        unordered_set<int> set2(nums2.begin(), nums2.end());

        vector<int> only1;
        for (int x : set1) {
            if (!set2.count(x)) only1.push_back(x);
        }

        vector<int> only2;
        for (int x : set2) {
            if (!set1.count(x)) only2.push_back(x);
        }

        return {only1, only2};
    }
};
class Solution:
    def findDifference(self, nums1: list[int], nums2: list[int]) -> list[list[int]]:
        set1 = set(nums1)
        set2 = set(nums2)

        only1 = [x for x in set1 if x not in set2]
        only2 = [x for x in set2 if x not in set1]

        return [only1, only2]
var findDifference = function(nums1, nums2) {
  const set1 = new Set(nums1);
  const set2 = new Set(nums2);

  const only1 = [];
  for (const x of set1) {
    if (!set2.has(x)) only1.push(x);
  }

  const only2 = [];
  for (const x of set2) {
    if (!set1.has(x)) only2.push(x);
  }

  return [only1, only2];
};

中文

题目概述

给你两个整数数组 nums1nums2,返回一个长度为 2 的数组: 第一个子数组是只在 nums1 中出现且不在 nums2 中出现的去重结果; 第二个子数组反之。

核心思路

本题本质是集合差集。先把两个数组转成集合完成去重,再分别计算 set1 - set2set2 - set1

算法步骤

1)由 nums1 构建集合 set1,由 nums2 构建集合 set2
2)遍历 set1,保留不在 set2 的元素。
3)遍历 set2,保留不在 set1 的元素。
4)返回 [only1, only2]

复杂度分析

时间复杂度:O(n + m)(平均哈希性能)。
空间复杂度:O(n + m)(集合与结果存储)。

常见陷阱

- 忘记“去重”要求,直接按原数组遍历会产生重复值。
- 输出顺序不保证固定(集合通常是无序结构)。
- 把“差集”写成“交集”逻辑,条件要仔细检查。

多语言参考实现(Java / Go / C++ / Python / JavaScript)

import java.util.*;

class Solution {
    public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();

        for (int x : nums1) set1.add(x);
        for (int x : nums2) set2.add(x);

        List<Integer> only1 = new ArrayList<>();
        for (int x : set1) {
            if (!set2.contains(x)) only1.add(x);
        }

        List<Integer> only2 = new ArrayList<>();
        for (int x : set2) {
            if (!set1.contains(x)) only2.add(x);
        }

        return Arrays.asList(only1, only2);
    }
}
func findDifference(nums1 []int, nums2 []int) [][]int {
    set1 := map[int]bool{}
    set2 := map[int]bool{}

    for _, x := range nums1 {
        set1[x] = true
    }
    for _, x := range nums2 {
        set2[x] = true
    }

    only1 := []int{}
    for x := range set1 {
        if !set2[x] {
            only1 = append(only1, x)
        }
    }

    only2 := []int{}
    for x := range set2 {
        if !set1[x] {
            only2 = append(only2, x)
        }
    }

    return [][]int{only1, only2}
}
class Solution {
public:
    vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int> set1(nums1.begin(), nums1.end());
        unordered_set<int> set2(nums2.begin(), nums2.end());

        vector<int> only1;
        for (int x : set1) {
            if (!set2.count(x)) only1.push_back(x);
        }

        vector<int> only2;
        for (int x : set2) {
            if (!set1.count(x)) only2.push_back(x);
        }

        return {only1, only2};
    }
};
class Solution:
    def findDifference(self, nums1: list[int], nums2: list[int]) -> list[list[int]]:
        set1 = set(nums1)
        set2 = set(nums2)

        only1 = [x for x in set1 if x not in set2]
        only2 = [x for x in set2 if x not in set1]

        return [only1, only2]
var findDifference = function(nums1, nums2) {
  const set1 = new Set(nums1);
  const set2 = new Set(nums2);

  const only1 = [];
  for (const x of set1) {
    if (!set2.has(x)) only1.push(x);
  }

  const only2 = [];
  for (const x of set2) {
    if (!set1.has(x)) only2.push(x);
  }

  return [only1, only2];
};

Comments