Skip to main content

30. Contains Duplicate

easyAsked at Vercel

Given an array, return true if any element appears more than once. Vercel asks this as the simplest set-based question; it's the warm-up before they pivot to streaming dedup or cache-key collisions.

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

Source citations

Public interview reports confirming this problem appears in Vercel loops.

  • Glassdoor (2025-Q4)Vercel screen warm-up; expected in 3 minutes.
  • LeetCode Discuss (2026-Q1)Listed in Vercel new-grad screen recap.

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. Sort then scan

Sort the array; check adjacent elements for equality.

Time
O(n log n)
Space
O(1) or O(n) depending on sort
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 the input. Slower than the Set version, but uses O(1) extra space (depending on sort).

2. Hash set early exit (optimal)

Add each element to a Set; if it's already there, return true.

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: O(n) time and space, single pass with early exit on first duplicate. The natural choice.

Vercel-specific tips

Vercel uses this as a baseline; getting it in 3 minutes signals you're moving cleanly. Bonus signal: discussing the trade-off between Set (O(n) space, O(n) time) and sort (O(1) space, O(n log n) time, mutates input), and connecting to streaming dedup with a Bloom filter for huge datasets.

Common mistakes

  • Using indexOf — O(n^2).
  • Forgetting early exit — adds every element, slower than necessary on duplicates near the start.
  • Using `nums.length !== new Set(nums).size` — works but doesn't early-exit and allocates the full Set.

Follow-up questions

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

  • Contains Duplicate II (LC 219) — within k indices.
  • Contains Duplicate III (LC 220) — within k indices and within t value distance.
  • Streaming dedup — Bloom filter or count-min sketch.

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 early exit?

If the first two elements are duplicates, the early-exit version returns in O(1). The `nums.length !== new Set(nums).size` shortcut forces full iteration even when an early answer exists.

When would sort be preferred?

Memory-constrained contexts where O(n) extra space is too much. Trade-off is O(n log n) time and input mutation.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →