All problems
1062EasyArrayHash Table

What Each List Holds Alone

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2215Find the Difference of Two Arrays

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a 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)
September 7
Apply