Skip to main content
Back to problems
Leetcode
Medium
Arrays
Hash Maps
Sets
Find The Difference Of Two Arrays

Return the unique values that appear in one array but not the other, in both directions.

Acceptance 0%
Problem Statement

Problem

You are given two integer arrays nums1 and nums2. Return the difference between the two arrays as two lists:

  • The first list contains the distinct values that appear in nums1 but do not appear in nums2.
  • The second list contains the distinct values that appear in nums2 but do not appear in nums1.

The order of values inside each list does not matter.

Goal

Compute the two set differences efficiently by identifying which values are present in each array and filtering out common values.

Input Format

  • nums1: an array of integers
  • nums2: an array of integers

Output Format

Return a list of two lists:

  • answer[0]: distinct integers in nums1 but not in nums2
  • answer[1]: distinct integers in nums2 but not in nums1

Constraints

  • 1 <= nums1.length, nums2.length <= 1000
  • -1000 <= nums1[i], nums2[i] <= 1000
  • Output values must be distinct within each list
  • Any order is acceptable
Examples
Sample cases returned by the problem API.

Example 1

Input

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

Output

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

Explanation

Values 1 and 3 are only in the first array, while 4 and 6 are only in the second array.

Example 2

Input

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

Output

[[3],[]]

Explanation

After removing duplicates, only 3 is exclusive to nums1; every value in nums2 also appears in nums1.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.