217. Contains Duplicate
easyAsked at IBMContains Duplicate is IBM's hash-set screener for SWE interns and SWE-1 phone screens. The interviewer is grading whether you reach for a Set in one pass, whether you can short-circuit early, and whether you name the sort vs hash space-time tradeoff cleanly.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in IBM loops.
- Glassdoor (2026-Q1)— Appears in IBM intern and SWE-1 phone-screen reports.
- LeetCode (2026-Q1)— Top-50 IBM-tagged problem.
- GeeksforGeeks (2025-11)— Listed in IBM interview-experience archive.
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]trueExample 2
nums = [1,2,3,4]falseExample 3
nums = [1,1,1,3,3,4,3,2,4,2]trueApproaches
1. Brute force — all pairs
Double loop checking nums[i] == nums[j] for i < j.
- Time
- O(n^2)
- Space
- O(1)
function containsDuplicateBrute(nums) {
for (let i = 0; i < nums.length; i++) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] === nums[j]) return true;
}
}
return false;
}Tradeoff: Constant extra space but O(n^2) — times out at 10^5. Useful as a baseline to immediately optimize.
2. Sort then scan adjacents
Sort the array, then walk it checking nums[i] == nums[i+1].
- Time
- O(n log n)
- Space
- O(1) in-place sort (typical V8 quicksort/timsort)
function containsDuplicateSort(nums) {
nums.sort((a, b) => a - b);
for (let i = 0; i < nums.length - 1; i++) {
if (nums[i] === nums[i + 1]) return true;
}
return false;
}Tradeoff: Useful when memory is tight and you can mutate the input. Mention it as the 'O(1) extra space' answer when the interviewer asks about memory.
3. Hash set (optimal)
Walk once. Track seen values in a Set. Return true the moment you see a repeat.
- 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: O(n) time at O(n) space. Short-circuits on the first repeat — for inputs with early duplicates this is near-constant. The canonical IBM answer; mentioning the sort alternative shows you considered the space tradeoff.
IBM-specific tips
IBM's phone screen rubric rewards the explicit tradeoff narration: 'I can sort in O(n log n) with O(1) extra space, or hash in O(n) with O(n) extra space.' The candidate who names the tradeoff before writing code outscores the one who just writes the hash set. Always state both options out loud.
Common mistakes
- Building the Set first, then checking length — wastes a pass when you could short-circuit on the first dup.
- Using Array.includes inside a loop — silently O(n^2) and the same as the brute force.
- Mutating the original array with sort when the problem says you shouldn't.
- Using an Object instead of a Set for the seen-values cache — slower lookups and risks prototype-key collisions on non-integer keys.
Follow-up questions
An interviewer at IBM may pivot to one of these next:
- Contains Duplicate II — return true if any two equal values are within distance k (LeetCode 219).
- Contains Duplicate III — within distance k AND absolute difference t (LeetCode 220).
- What if the array is too large to fit in memory? (Streaming + Bloom filter, or external sort.)
- Find the duplicate number in [1..n] in O(1) extra space (LeetCode 287).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
When does sort beat hash for this problem?
When memory is constrained (embedded systems, streaming with bounded memory) or when the input is already sorted (the cost is just the linear scan). On a vanilla phone screen, hash wins because it short-circuits on early duplicates and ships in 4 lines.
What if the array contains 64-bit integers or BigInts?
JavaScript Sets handle BigInt natively; the answer is unchanged. For arrays of arrays or arrays of objects, you'd serialize to a stable key (JSON.stringify with sorted keys) or use a custom hash.
Free learning resources
Curated free links for this problem.
Practice these live with InterviewChamp.AI
Drill Contains Duplicate and other IBM interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →