Find the Difference of Two Arrays
Real-world version: What Each List Holds AloneThe 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.
def find_difference(left: list[int], right: list[int]) -> list[list[int]]:public List<List<Integer>> findDifference(int[] left, int[] right)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 17 test cases: 17 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's