Sets
A set stores unique values — adding the same value twice changes nothing — and tests whether a value is present almost instantly. JavaScript has Set built in.
const seen = new Set()
seen.add(1)
seen.add(1) // ignored, already there
console.log(seen.has(1)) // true
console.log(seen.size) // 1
The quickest way to remove duplicates from an array is a set:
Try it yourself
Loading editor…
Checking "have I seen this before?" with a set is O(1). Doing the same with includes() on an array is O(n) every time.