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 integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2] Output: [2]
Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] Output: [9,4] Explanation: [4,9] is also accepted.
Constraints:
1 <= nums1.length, nums2.length <= 10000 <= nums1[i], nums2[i] <= 1000Problem summary: Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Two Pointers · Binary Search
[1,2,2,1] [2,2]
[4,9,5] [9,4,9,8,4]
intersection-of-two-arrays-ii)intersection-of-three-sorted-arrays)find-the-difference-of-two-arrays)count-common-words-with-one-occurrence)choose-numbers-from-two-arrays-in-range)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #349: Intersection of Two Arrays
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
boolean[] s = new boolean[1001];
for (int x : nums1) {
s[x] = true;
}
List<Integer> ans = new ArrayList<>();
for (int x : nums2) {
if (s[x]) {
ans.add(x);
s[x] = false;
}
}
return ans.stream().mapToInt(Integer::intValue).toArray();
}
}
// Accepted solution for LeetCode #349: Intersection of Two Arrays
func intersection(nums1 []int, nums2 []int) (ans []int) {
s := [1001]bool{}
for _, x := range nums1 {
s[x] = true
}
for _, x := range nums2 {
if s[x] {
ans = append(ans, x)
s[x] = false
}
}
return
}
# Accepted solution for LeetCode #349: Intersection of Two Arrays
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
return list(set(nums1) & set(nums2))
// Accepted solution for LeetCode #349: Intersection of Two Arrays
struct Solution;
use std::collections::HashSet;
impl Solution {
fn intersection(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
let h1: HashSet<i32> = nums1.into_iter().collect();
let h2: HashSet<i32> = nums2.into_iter().collect();
let bitand = &h1 & &h2;
bitand.into_iter().collect()
}
}
#[test]
fn test() {
let nums1 = vec![1, 2, 2, 1];
let nums2 = vec![2, 2];
assert_eq!(Solution::intersection(nums1, nums2), vec![2]);
}
// Accepted solution for LeetCode #349: Intersection of Two Arrays
function intersection(nums1: number[], nums2: number[]): number[] {
const s = new Set(nums1);
return [...new Set(nums2.filter(x => s.has(x)))];
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
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.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.