Skip to main content

29. Contains Duplicate

easyAsked at Plaid

Determine if any value appears at least twice in an array. Plaid asks this because detecting duplicate webhook deliveries or duplicate idempotency keys is exactly this primitive.

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

Source citations

Public interview reports confirming this problem appears in Plaid loops.

  • Glassdoor (2025)Plaid backend OA — webhook dedup.
  • LeetCode Discuss (2026)Plaid intro.

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 and adjacent check

Sort, then check if any adjacent pair is equal.

Time
O(n log n)
Space
O(1) if in-place 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 input. O(n log n) when O(n) is achievable.

2. Hash set

Walk once, add to set, return true on first collision.

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 with extra space. Short-circuits on first duplicate — important when most arrays do have duplicates.

Plaid-specific tips

Plaid grades this on whether you short-circuit on first match. Bonus signal: connect to their idempotency-key store — if a webhook arrives with an idempotency key already in Redis, skip it. The pattern is identical.

Common mistakes

  • Using new Set(nums).size !== nums.length — walks the whole array even when the duplicate is at index 1.
  • Using indexOf in a loop — O(n^2).
  • Sorting and then comparing without breaking early.

Follow-up questions

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

  • Contains duplicate within k positions (LC 219).
  • Contains duplicate within k positions with value-difference <= t (LC 220).
  • Stream version — bounded memory dedup.

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 prefer hash set over Set(nums).size?

The size comparison forces a full scan. The loop-and-bail version returns on the first duplicate, which is the common case in webhook dedup.

What's the bounded-memory variant for streams?

Bloom filter — false-positive but constant memory. Or LRU cache of recent idempotency keys — what Plaid uses in practice.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →