Free practice lab
Code Comparison
Compare two implementations of the same task and justify which is better under the stated constraints. Free — no account needed.
Free practice lab
Compare two implementations of the same task and justify which is better under the stated constraints. Free — no account needed.
Cookies on this site
We use essential cookies to run the questionnaire, practice labs and sign-in. With your permission we'd also like to use analytics to understand which pages help people. Rejecting never limits any feature. Cookie policy
Two correct ways to check an array for duplicate integers, with very different costs at scale.
Write containsDuplicate(nums) that returns true if any integer appears more than once in nums, otherwise false.
nums contains between 0 and 100,000 integers.
Values range from -1,000,000,000 to 1,000,000,000.
The function runs in a request handler, so response time matters.
Memory is not tightly limited: a few megabytes of extra memory is acceptable.
function containsDuplicate(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;}function containsDuplicate(nums) { const seen = new Set(); for (const n of nums) { if (seen.has(n)) return true; seen.add(n); } return false;}| Input | Expected | Note |
|---|---|---|
| [1, 2, 3, 1] | true | Duplicate at the ends |
| [1, 2, 3, 4] | false | |
| [] | false | Empty input |
| [7] | false | Single element |
| [-1, 0, -1] | true | Negative values |