Skip to main content

74. Contains Duplicate

easyAsked at Workday

Determine whether an array contains any duplicates. Workday uses this for hash-set fluency — same shape as checking whether an employee ID appears twice in a payroll batch.

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

Source citations

Public interview reports confirming this problem appears in Workday loops.

  • Glassdoor (2025)Workday SDE1 phone screen warmup.

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

Sort the array; any adjacent equal pair is a dup.

Time
O(n log n)
Space
O(1) if mutable)
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).

2. Hash set early exit

Insert each into a set; first collision returns true.

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

Tradeoff: Single pass with early exit. Trades O(n) space for O(n) time.

Workday-specific tips

Workday wants the hash-set version. Mention that the early exit saves work — if a duplicate appears early, we don't scan the rest. Also: if memory is tight, mention the sort variant as the O(1)-space alternative.

Common mistakes

  • Using a Set and returning seen.size !== nums.length — works but reads the full array even when a dup is at index 1.
  • Sorting when O(n) is achievable.
  • Using nested loops for an O(n^2) check.

Follow-up questions

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

  • Contains Duplicate II (LC 219) — within k indices apart.
  • Contains Duplicate III (LC 220) — values within t and indices within k.
  • What if the array is too large to fit in memory?

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?

Returning on first collision avoids walking the rest. Important when nums is huge.

Sort or set?

Set is O(n) time / O(n) space. Sort is O(n log n) time / O(1) space if mutating allowed. Pick based on the constraint that matters.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →