---
title: "How do I remove duplicates from an array in JavaScript?"
description: "Use a Set to remove duplicate primitive values in one line. For arrays of objects, deduplicate by a key with a Map. Here is when to use each approach."
url: https://www.danny.engineering/answer/javascript/how-do-i-remove-duplicates-from-an-array-in-javascript
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["Javascript"]
---

# How do I remove duplicates from an array in JavaScript?

## The short answer


For strings, numbers and other primitive values, pass the array to a `Set` and spread it back into an array. A `Set` only keeps one copy of each value and keeps the order of first appearance. For objects, deduplicate by a key such as `id` instead.


```javascript
const tags = ['js', 'css', 'js', 'html', 'css'];
const unique = [...new Set(tags)];
// ['js', 'css', 'html']
```


## Why it happens


JavaScript arrays allow repeated values, so duplicates usually come from merging lists, concatenating API pages, or pushing items in a loop. Removing them depends on what "the same" means:

- Primitive values are equal when they have the same value, so `Set` works directly.
- Objects are compared by reference. Two objects with the same fields are still different values, so `new Set(users)` keeps both. You need to choose a key that identifies a duplicate.

`Set` uses the SameValueZero comparison. That means `NaN` matches `NaN`, and `0` matches `-0`, which is usually what you want.


## How to fix it


### Primitives: use a Set


This is the shortest and fastest option for primitives, and it runs in linear time.


```javascript
const ids = [3, 1, 3, 2, 1];
const uniqueIds = Array.from(new Set(ids));
// [3, 1, 2]
```


`Array.from` and the spread syntax do the same thing here; pick whichever reads better to you.


### Objects: deduplicate by a key with a Map


Build a `Map` keyed by the identifying property, then take its values. With this version, the last item with a given key wins.


```javascript
const users = [
  { id: 1, name: 'Ada' },
  { id: 2, name: 'Grace' },
  { id: 1, name: 'Ada L.' },
];

const byId = [...new Map(users.map((user) => [user.id, user])).values()];
// [{ id: 1, name: 'Ada L.' }, { id: 2, name: 'Grace' }]
```


To keep the first item instead, track the keys you have seen:


```javascript
function uniqueBy(items, getKey) {
  const seen = new Set();
  return items.filter((item) => {
    const key = getKey(item);
    if (seen.has(key)) return false;
    seen.add(key);
    return true;
  });
}

const firstById = uniqueBy(users, (user) => user.id);
```


The key function can combine fields too, for example `(item) => item.date + '|' + item.room`.


### Using filter and indexOf


You will often see this older pattern. It works for primitives, but it scans the array for every item, so it gets slow on large arrays.


```javascript
const unique = tags.filter((tag, index) => tags.indexOf(tag) === index);
```


It also fails to remove repeated `NaN` values, because `indexOf` never finds `NaN`. Prefer `Set` unless you need to support very old environments.


### Case-insensitive or trimmed values


Normalise the value and use it as the key so that `'React'` and `'react '` count as the same.


```javascript
const cleaned = uniqueBy(names, (name) => name.trim().toLowerCase());
```


## Common pitfalls

- Expecting `new Set(objects)` to remove objects that look the same. It only removes the exact same reference.
- Using `JSON.stringify` as a key for whole objects. It is slow and treats the same fields in a different order as different.
- Forgetting that the `Map` version keeps the last match while `uniqueBy` keeps the first. Choose on purpose.
- Mutating the original array. All the approaches above return a new array and leave the input unchanged.

## Related reading

- [MDN: Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)
- [MDN: Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)
