Skip to main content

217. Contains Duplicate

easyAsked at Glean

Glean uses this as a fast filter to test whether candidates reach for a set before a nested loop — the exact reasoning behind deduplication in search indexing pipelines.

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

Source citations

Public interview reports confirming this problem appears in Glean loops.

  • Glassdoor (2025-10)Reported as a 5-minute warm-up in Glean early-round screens before candidates move to medium problems.
  • Blind (2025-08)Glean SWE threads mention Contains Duplicate as a quick sanity-check problem in phone screens.

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

Explanation: 1 appears at index 0 and index 3.

Example 2

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

Explanation: All elements are distinct.

Example 3

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

Explanation: Multiple duplicates exist.

Approaches

1. Hash set

Iterate through the array. For each element, check if it's already in a set. If yes, return true. Otherwise, add it to the set. Return false after the loop.

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

Tradeoff: O(n) time, O(n) space. The canonical answer — early return means you stop the moment a duplicate is found, which is optimal in practice.

2. Sort then scan

Sort the array. Duplicates will be adjacent. Scan once for any adjacent equal pair.

Time
O(n log n)
Space
O(1) or O(log n) for sort stack
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: Worse time complexity but O(1) extra space (modifies input). Useful if space is the primary constraint. Mention as a trade-off but prefer the set approach.

Glean-specific tips

This is a warm-up — solve it quickly and cleanly. Use a Set, early-return on the first duplicate, and explain the trade-off between the O(n) set approach and the O(n log n) sort approach in one sentence. Glean interviewers use this to assess communication speed as much as algorithm knowledge.

Common mistakes

  • Using nums.indexOf() inside a loop — O(n²) total; never use indexOf as a membership check.
  • Comparing nums.length to new Set(nums).size — works but doesn't early-return; the iterative set approach is more efficient.
  • Sorting without copying the array when the caller expects the original to be unchanged — clarify whether mutation is allowed.
  • Not reading the problem carefully — 'at least twice' means any duplicate at all, not exactly twice.

Follow-up questions

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

  • Contains Duplicate II (LC 219) — duplicate must be within k indices of each other.
  • Contains Duplicate III (LC 220) — values must be within a range t of each other, within k indices.
  • Find All Duplicates in an Array (LC 442) — return all duplicates in O(n) time and O(1) extra space.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Is new Set(nums).size < nums.length a valid one-liner?

Yes and it passes tests, but it always iterates the entire array. The explicit loop with early return is faster when a duplicate is near the front.

What if the array can contain non-integer values?

JavaScript's Set works with any value type (strings, objects by reference, etc.), so the same approach applies — just ensure you're comparing the right thing.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →