217. Contains Duplicate
easyAsked at eBayeBay uses duplicate detection extensively — deduplicating search results, preventing double-listings, and fraud detection all rely on fast membership checks. Contains Duplicate is the simplest form of this pattern and tests whether you instinctively reach for a Set over a nested loop when you need O(n) duplicate detection.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in eBay loops.
- Glassdoor (2025-11)— Mentioned as a warm-up problem in eBay phone screens for junior SWE candidates.
- Blind (2025-08)— eBay threads cite Contains Duplicate as an early phone-screen filter before more complex algorithm questions.
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
nums = [1,2,3,1]trueExplanation: The value 1 appears at index 0 and index 3.
Example 2
nums = [1,2,3,4]falseExplanation: All values are distinct.
Example 3
nums = [1,1,1,3,3,4,3,2,4,2]trueExplanation: Multiple values appear more than once.
Approaches
1. Hash Set
Iterate through the array. For each element, check if it is already in a Set. If yes, return true. Otherwise, add it to the Set. If the loop completes, return false.
- 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 — optimal time complexity. This is the expected answer. Can also be written as a one-liner: return nums.length !== new Set(nums).size — but the explicit loop demonstrates understanding of early exit.
2. Sort then scan
Sort the array; duplicates will be adjacent. Scan for adjacent equal elements.
- Time
- O(n log n)
- Space
- O(1) (in-place sort) or O(n) (stable 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: O(n log n) time — worse than Set approach but O(1) extra space if sorting in place. Useful when space is more constrained than time, but mutates the input array.
eBay-specific tips
This problem has a trivial one-liner in JavaScript (new Set(nums).size !== nums.length), but eBay interviewers prefer you to code the explicit loop to demonstrate awareness of early exit — returning true as soon as a duplicate is found avoids scanning the rest of the array. Also mention the space-time tradeoff: 'If space is scarce, I can sort first and scan adjacent elements for O(1) extra space at the cost of O(n log n) time.'
Common mistakes
- Using the one-liner without explaining it — interviewers want to see that you understand what Set does, not just that you know the shortcut.
- Not mentioning early exit as an advantage over the Set-size comparison — building the full Set first doesn't short-circuit.
- Sorting with default lexicographic sort (nums.sort()) — this sorts numbers as strings and gives wrong results; always use a comparator.
- Forgetting that sort mutates the input array — relevant if the caller needs the original order preserved.
Follow-up questions
An interviewer at eBay may pivot to one of these next:
- Contains Duplicate II (LC 219) — duplicates within k indices of each other; use a sliding-window Set.
- Contains Duplicate III (LC 220) — duplicates within a value range of t and index range of k; use a sorted set or buckets.
- How would you find all duplicates, not just detect whether one exists? (LC 442)
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
What is the space cost of the Set approach?
O(n) in the worst case — when all elements are distinct, every element is added to the Set before returning false.
Is the one-liner (new Set(nums).size !== nums.length) acceptable?
Yes, but explain it. It builds the full Set (no early exit) and compares sizes. The explicit loop is preferred for clarity and performance when duplicates appear early.
Can this be solved in O(1) space without sorting?
Not in the general case with arbitrary integer values. If values were bounded to [0, n-1], you could use the array itself as a visited marker (negating elements at index nums[i]).
Practice these live with InterviewChamp.AI
Drill Contains Duplicate and other eBay interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →