Skip to main content

5. Contains Duplicate

easyAsked at Square

Given an array, return whether any value appears at least twice. Square uses this trivial-seeming problem as a habit check — they want the Set reach, not a nested loop, because their idempotency-key pipelines depend on the same instinct.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Square loops.

  • LeetCode Discuss (2025)Square POS phone screen warm-up.
  • Glassdoor (2026)Cash App SRE screen.

Problem

Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9

Examples

Example 1

Input
nums = [1,2,3,1]
Output
true

Example 2

Input
nums = [1,2,3,4]
Output
false

Example 3

Input
nums = [1,1,1,3,3,4,3,2,4,2]
Output
true

Approaches

1. Brute force pair comparison

Compare every pair (i, j); return true on the first match.

Time
O(n^2)
Space
O(1)
function containsDuplicate(nums) {
  for (let i = 0; i < nums.length; i++) {
    for (let j = i + 1; j < nums.length; j++) {
      if (nums[i] === nums[j]) return true;
    }
  }
  return false;
}

Tradeoff: TLEs at 10^5; demonstrates the anti-pattern.

2. Set early-exit

Walk once and add each value to a Set. The first time .has(v) is true, return.

Time
O(n)
Space
O(n)
function containsDuplicate(nums) {
  const seen = new Set();
  for (const n of nums) {
    if (seen.has(n)) return true;
    seen.add(n);
  }
  return false;
}

Tradeoff: Linear time. Early-exit matters — don't add everything then check size, that wastes the savings on a duplicate at index 1.

Square-specific tips

Square interviewers expect you to mention early-exit explicitly. They like candidates who articulate 'check before insert' rather than 'insert all, then compare size to length' — the former mirrors their idempotency-key checks for payment requests where the second duplicate request must be rejected immediately, not after the whole batch processes.

Common mistakes

  • Adding everything to a Set then comparing size — wastes the early-exit benefit on early duplicates.
  • Using sort + compare adjacent — that's O(n log n), worse than O(n) Set.
  • Using an object {} which stringifies keys and may collide on negative numbers vs strings.

Follow-up questions

An interviewer at Square may pivot to one of these next:

  • Contains Duplicate II (LC 219): duplicates within k distance.
  • Contains Duplicate III (LC 220): values within t and indices within k.
  • How would you implement this on a stream you can't replay?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why is Set O(1) per operation?

Hash-backed lookup. Worst case is O(n) on adversarial collisions, but JS engines pick non-deterministic hashes to defend against that.

Can I use the array's index trick?

Only if values are constrained to [0, n). Not the case here — values go to 10^9.

Practice these live with InterviewChamp.AI

Drill Contains Duplicate and other Square interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →