Full answer
The fastest and clearest way for primitive values is a Set: [...new Set(items)], or Array.from(new Set(items)). A Set keeps only unique values, and keeps the order of first insertion. It uses SameValueZero equality, so NaN equals NaN (unlike ===), and +0 and -0 are the same value.
The older approach is items.filter((x, i) => items.indexOf(x) === i). It also keeps the first occurrence, but indexOf scans the array each time, so it is O(n squared). It also never finds NaN, because indexOf uses strict equality, so every NaN is dropped.
For an array of objects, a Set does not work, because two objects with the same content are different references. Choose the key that defines "same", for example id, and track the keys you have seen: loop over the items, and keep an item if its key is not in the seen Set. To keep the last occurrence instead, build a Map from key to item and let later items overwrite earlier ones: [...new Map(items.map((x) => [x.id, x])).values()]. Note that this keeps the position of the first insertion of the key, but the value of the last item.
Comparing whole objects by content needs a canonical key such as JSON.stringify with sorted keys, or a deep equal function. JSON.stringify depends on property order and cannot represent some values, so use it only for simple data.
State what is kept (first or last) and whether the original array may change. These methods create a new array and do not mutate the input.
const unique = (items) => [...new Set(items)];
const uniqueByIndexOf = (items) => items.filter((x, i) => items.indexOf(x) === i);
const uniqueBy = (items, keyFn) => {
const seen = new Set();
return items.filter((item) => {
const key = keyFn(item);
if (seen.has(key)) return false;
seen.add(key);
return true;
});
};
const uniqueByKeepLast = (items, keyFn) => [...new Map(items.map((item) => [keyFn(item), item])).values()];
console.log(unique([1, 2, 2, 3, 1, '1']));
console.log(unique([NaN, NaN, 0, -0]), uniqueByIndexOf([NaN, NaN, 1, 1]));
const users = [
{ id: 1, name: 'Asha' },
{ id: 2, name: 'Ben' },
{ id: 1, name: 'Asha (updated)' },
];
console.log(uniqueBy(users, (u) => u.id));
console.log(uniqueByKeepLast(users, (u) => u.id));
console.log(unique([{ a: 1 }, { a: 1 }]).length);
const sortedKey = (o) => JSON.stringify(Object.entries(o).sort(([a], [b]) => (a < b ? -1 : 1)));
console.log(uniqueBy([{ a: 1, b: 2 }, { b: 2, a: 1 }, { a: 1, b: 3 }], sortedKey));Run with Node.js v24.22.1 as js-119.js; two runs gave identical output. Output: [ 1, 2, 3, '1' ] [ NaN, 0 ] [ 1 ] [ { id: 1, name: 'Asha' }, { id: 2, name: 'Ben' } ] [ { id: 1, name: 'Asha (updated)' }, { id: 2, name: 'Ben' } ] 2 [ { a: 1, b: 2 }, { a: 1, b: 3 } ]
Key points
- [...new Set(items)] is the shortest and fastest for primitives; it keeps the first occurrence.
- Set treats NaN as equal to NaN; indexOf cannot find NaN.
- filter with indexOf is O(n squared).
- For objects, track a key in a Set to keep the first, or use a Map to keep the last.
- Content comparison needs a canonical key or a deep equal function.
Common mistakes
- Using a Set on objects and expecting content comparison.
- Using filter with indexOf on large arrays.
- Expecting indexOf to handle NaN.
- Not stating whether the first or the last duplicate should win.
Follow-ups
- What is the difference between SameValueZero and strict equality?
- How would you remove duplicates in place without creating a new array?
- How would you deduplicate a very large stream without keeping all values in memory?
Tips
- Start with the Set one-liner, then explain the objects case.
- Mention first versus last occurrence.
- Know that indexOf misses NaN.