Skip to main content

75. Contains Duplicate

easyAsked at Reddit

Determine if an array contains duplicate values. Reddit uses this as a hash-set warm-up — the same primitive used to detect duplicate vote events arriving from sharded gateways.

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

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit phone-screen warm-up.

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 + adjacent compare

Sort; compare consecutive pairs.

Time
O(n log n)
Space
O(1)
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: O(n log n) — beats brute force but slower than hash.

2. Hash set (optimal)

Walk array; check set membership; return true on first hit.

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, single pass, early exit. The canonical answer.

Reddit-specific tips

Reddit interviewers want hash-set immediately. Bonus signal: mention Set's size === nums.length one-liner as a Pythonic alternative and connect to dedup on vote-event ingest.

Common mistakes

  • Returning false when set.size === nums.length (works but verbose; the linear early-exit is better).
  • Using Array.includes inside the loop (O(n^2)).

Follow-up questions

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

  • Contains Duplicate II (LC 219) — within k distance.
  • Contains Duplicate III (LC 220) — within k distance and abs(diff) <= t.
  • Find duplicates in O(1) space (LC 287).

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

When is sort preferred?

When you also need to know which value, in sorted order, or when memory is tight.

Could a Bloom filter help?

Probabilistic — has false positives. Useful at huge scale where exactness can be relaxed.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →