1. Seen set
A Set gives O(1) membership tests with no duplicate keys.
const seen = new Set()Typorithm · easy
Step 1 of 8 · intro card
Arrays & Hashing
Track seen values in a Set; the first repeat returns true, otherwise return false.
A Set membership test is O(1), so scanning once is enough.
Each value is checked, then added; a hit on the check means a duplicate.
The early exit stops as soon as the first repeat is found.
Set membership is O(1).
Check, then add.
Early-exit on the first repeat.
function containsDuplicate(nums) {
const seen = new Set();
for (const num of nums) {
if (seen.has(num)) {
return true;
}
seen.add(num);
}
return false;
}Connect each part of the code to the concept before you type.
A Set gives O(1) membership tests with no duplicate keys.
const seen = new Set()If the value is already present, a duplicate exists.
if (seen.has(num))Record the value so later repeats are caught.
seen.add(num)Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.