How do you remove duplicate values from an array?
For primitive values the modern answer is a Set. A set keeps only one copy of each value, and spreading it back into an array gives you the result in one line.
const numbers = [1, 2, 2, 3, 3, 3, 4];
console.log([...new Set(numbers)]); // [1, 2, 3, 4]
// The older filter approach does the same thing:
console.log(numbers.filter((n, i) => numbers.indexOf(n) === i)); // [1, 2, 3, 4]The Set version is the one to reach for. It runs in linear time because the set does the lookup for you, whereas the filter version calls indexOf for every element, which is quadratic on a large array. The filter version is worth recognising because it is still common in older code, and because it works with the same predicate style.
Two important limits. A Set uses the same-value-zero comparison, so it treats values that are strictly equal, plus the special case where positive zero and negative zero are treated as the same. It does not do anything clever with objects, because two distinct object literals are never the same value, so a set of objects will keep them all.
And when the values are objects, you need to decide what makes two of them duplicates. Usually it is a key, such as an id, and then you need a different approach entirely.