10. Contains Duplicate
easyAsked at ByteDanceReturn true if any value appears twice in the array — ByteDance uses it as a dedup warm-up before deeper content-fingerprinting questions.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given an integer array nums, return true if any value appears at least twice 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]falseApproaches
1. Sort and compare neighbors
Sort the array and look for any adjacent duplicates.
- Time
- O(n log n)
- Space
- O(1)
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:
2. Hash set
Stream through nums; bail the moment a value is already in the set.
- 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:
ByteDance-specific tips
ByteDance reviewers reward the framing that this is the simplest possible dedup, which ties directly to how their video-upload pipeline rejects re-uploaded clips by perceptual hash.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Contains Duplicate and other ByteDance interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →