10. Contains Duplicate
easyAsked at CoupangDetect whether any value appears twice in an array, mirroring how Coupang's returns-processing pipeline deduplicates RMA submissions.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given an integer array nums, return true if any value appears at least twice and 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. Brute force
Check every pair.
- Time
- O(n^2)
- Space
- O(1)
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:
2. Hash set
Insert each value into a set; if it's already present, you've found a duplicate.
- 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:
Coupang-specific tips
Coupang returns-processing rejects duplicate RMA submissions; the Set-based O(n) sweep is the standard pattern in their backend codebase.
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 Coupang interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →