Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where:
answer[0] is a list of all distinct integers in nums1 which are not present in nums2.answer[1] is a list of all distinct integers in nums2 which are not present in nums1.Note that the integers in the lists may be returned in any order.
Example 1:
Input: nums1 = [1,2,3], nums2 = [2,4,6] Output: [[1,3],[4,6]] Explanation: For nums1, nums1[1] = 2 is present at index 0 of nums2, whereas nums1[0] = 1 and nums1[2] = 3 are not present in nums2. Therefore, answer[0] = [1,3]. For nums2, nums2[0] = 2 is present at index 1 of nums1, whereas nums2[1] = 4 and nums2[2] = 6 are not present in nums1. Therefore, answer[1] = [4,6].
Example 2:
Input: nums1 = [1,2,3,3], nums2 = [1,1,2,2] Output: [[3],[]] Explanation: For nums1, nums1[2] and nums1[3] are not present in nums2. Since nums1[2] == nums1[3], their value is only included once and answer[0] = [3]. Every integer in nums2 is present in nums1. Therefore, answer[1] = [].
Constraints:
1 <= nums1.length, nums2.length <= 1000-1000 <= nums1[i], nums2[i] <= 1000Problem summary: Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where: answer[0] is a list of all distinct integers in nums1 which are not present in nums2. answer[1] is a list of all distinct integers in nums2 which are not present in nums1. Note that the integers in the lists may be returned in any order.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,2,3] [2,4,6]
[1,2,3,3] [1,1,2,2]
intersection-of-two-arrays)intersection-of-two-arrays-ii)intersection-of-multiple-arrays)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2215: Find the Difference of Two Arrays
class Solution {
public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
Set<Integer> s1 = convert(nums1);
Set<Integer> s2 = convert(nums2);
List<Integer> l1 = new ArrayList<>();
List<Integer> l2 = new ArrayList<>();
for (int v : s1) {
if (!s2.contains(v)) {
l1.add(v);
}
}
for (int v : s2) {
if (!s1.contains(v)) {
l2.add(v);
}
}
return List.of(l1, l2);
}
private Set<Integer> convert(int[] nums) {
Set<Integer> s = new HashSet<>();
for (int v : nums) {
s.add(v);
}
return s;
}
}
// Accepted solution for LeetCode #2215: Find the Difference of Two Arrays
func findDifference(nums1 []int, nums2 []int) [][]int {
s1, s2 := make(map[int]bool), make(map[int]bool)
for _, v := range nums1 {
s1[v] = true
}
for _, v := range nums2 {
s2[v] = true
}
ans := make([][]int, 2)
for v := range s1 {
if !s2[v] {
ans[0] = append(ans[0], v)
}
}
for v := range s2 {
if !s1[v] {
ans[1] = append(ans[1], v)
}
}
return ans
}
# Accepted solution for LeetCode #2215: Find the Difference of Two Arrays
class Solution:
def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:
s1, s2 = set(nums1), set(nums2)
return [list(s1 - s2), list(s2 - s1)]
// Accepted solution for LeetCode #2215: Find the Difference of Two Arrays
use std::collections::HashSet;
impl Solution {
pub fn find_difference(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<Vec<i32>> {
vec![
nums1
.iter()
.filter_map(|&v| if nums2.contains(&v) { None } else { Some(v) })
.collect::<HashSet<i32>>()
.into_iter()
.collect(),
nums2
.iter()
.filter_map(|&v| if nums1.contains(&v) { None } else { Some(v) })
.collect::<HashSet<i32>>()
.into_iter()
.collect(),
]
}
}
// Accepted solution for LeetCode #2215: Find the Difference of Two Arrays
function findDifference(nums1: number[], nums2: number[]): number[][] {
const s1: Set<number> = new Set(nums1);
const s2: Set<number> = new Set(nums2);
nums1.forEach(num => s2.delete(num));
nums2.forEach(num => s1.delete(num));
return [Array.from(s1), Array.from(s2)];
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.