Skip to main content

6. Contains Duplicate

easyAsked at Snap

Return true if any value appears at least twice in the array. Snap uses this to confirm the candidate reaches for a hash set rather than nested loops.

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

Source citations

Public interview reports confirming this problem appears in Snap loops.

  • Glassdoor (2026-Q1)Snap reports cite this as a 5-minute screen problem.
  • LeetCode Discuss (2025)Frequently paired with Valid Anagram in Snap warm-ups.

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

Approaches

1. Sort then scan

Sort the array, then compare consecutive elements.

Time
O(n log n)
Space
O(1) or O(n)
function containsDuplicate(nums) {
  nums.sort((a, b) => a - b);
  for (let i = 1; i < nums.length; i++) {
    if (nums[i] === nums[i - 1]) return true;
  }
  return false;
}

Tradeoff: Mutates input and is O(n log n) — strictly worse than the set approach unless space is critical.

2. Hash set short-circuit (optimal)

Walk the array adding each value to a set. The instant you see a duplicate insertion attempt, return true.

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

Tradeoff: Single pass with early exit. Standard cache pattern Snap will expect.

Snap-specific tips

Snap interviewers expect you to vocalize the cache-vs-sort tradeoff. Bonus signal: connect this to deduping user-uploaded Snap media — Snap dedupes by content hash, the same pattern at a much larger scale.

Common mistakes

  • Adding all elements before checking — skips the early-exit speedup.
  • Using nums.indexOf inside a loop — silently O(n^2).
  • Forgetting that JS Set treats NaN as equal to itself — usually fine here.

Follow-up questions

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

  • Contains Duplicate II — duplicates within distance k (LC 219).
  • Contains Duplicate III — values within t and distance k (LC 220).
  • What if memory is tight? Sort or use a Bloom filter.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Does this work for floats?

Yes, but watch for NaN. JS Set has special NaN handling so two NaNs collide; other languages might differ.

Should I use Array.prototype.has?

Array has no native O(1) lookup — that's why we use Set. Using Array.includes inside the loop reintroduces O(n^2).

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →