new Set()
Pour the list into a Set — which can't hold duplicates — then spread it back into an array.
const unique = function (list) { return [...new Set(list)]; }; unique([3, 1, 3, 2, 1]); // → [3, 1, 2]
Pros
- +Shortest to read and write — one line.
- +Fast: roughly linear, scales to big lists.
- +Keeps original order; handles
NaN.
Cons
- –De-dupes by identity only — no custom "same".
- –Objects compared by reference, not contents.