1. Dedup with Set
Passing an array to new Set drops duplicates, keeping each value once.
new Set([1, 2, 2, 3])Typorithm · medium
Step 1 of 8 · intro card
Objects & Maps
A Set stores unique values: new Set(arr) deduplicates, has gives O(1) membership, and add/delete mutate it. Spread back to an array with [...s], and build union and intersection with spread and filter.
A Set holds each value at most once, so new Set(array) is the idiomatic one-liner for removing duplicates.
Membership with set.has(x) is O(1), far faster than array.includes on large data, and add/delete keep the Set up to date.
Sets are not arrays — spread with [...set] to get an array back — and you compose them: union is new Set([...a, ...b]) while intersection is new Set([...a].filter((x) => b.has(x))).
new Set(arr) deduplicates values.
has, add, and delete are O(1) operations.
Spread + filter build union and intersection.
const unique = new Set([1, 2, 2, 3])
const present = unique.has(2)
unique.add(4)
unique.delete(1)
const arr = [...unique]
const a = new Set([1, 2, 3])
const b = new Set([2, 3, 4])
const union = new Set([...a, ...b])
const intersection = new Set([...a].filter((x) => b.has(x)))Connect each part of the code to the concept before you type.
Passing an array to new Set drops duplicates, keeping each value once.
new Set([1, 2, 2, 3])has(value) is an O(1) check for whether the value is in the Set.
unique.has(2)add inserts a value; adding an existing value is a no-op.
unique.add(4)delete removes a value and returns whether it was present.
unique.delete(1)Spreading a Set into an array is the standard way to materialize its values.
[...unique]Spreading both Sets into one new Set yields their union with duplicates removed.
new Set([...a, ...b])Filtering a's values by b.has keeps only the values present in both Sets.
new Set([...a].filter((x) => b.has(x)))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.