Show problems as
Classic problem #2215EasyArrayHash Table

Find the Difference of Two Arrays

Real-world version: What Each List Holds Alone
Tracked in this browser only
Read Find the Difference of Two Arrays on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Two lists of whole numbers are given, left and right. Either list may hold repeats.

Return a list of two lists. The first holds every value that appears in left but nowhere in right, and the second holds every value that appears in right but nowhere in left. Each value appears once in its list, however often it appeared in the input, and both lists come out in increasing order.

Examples

Example 1

Input
left = [4, 5, 6], right = [5, 7]
Output
[[4, 6], [7]]

The 5 sits in both lists, so it appears in neither answer. That leaves 4 and 6 held only on the left, and 7 only on the right.

Example 2

Input
left = [1, 1, 1], right = [1]
Output
[[], []]

The only value either list holds is 1, and both hold it, so both answers come out empty however many times it was repeated.

Example 3

Input
left = [9], right = [8]
Output
[[9], [8]]

The lists share nothing, so each reports its own single value.

Constraints

  • 1 <= left.length <= 1000
  • 1 <= right.length <= 1000
  • -1000 <= left[i] <= 1000
  • -1000 <= right[i] <= 1000

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def find_difference(left: list[int], right: list[int]) -> list[list[int]]:
Java
public List<List<Integer>> findDifference(int[] left, int[] right)