---
title: "How do you remove duplicates from an array in JavaScript?"
description: "Remove duplicate values from a JavaScript array with a Set, and deduplicate arrays of objects by a key using a Map."
url: "https://bugsee.com/answers/javascript/remove-duplicates-from-an-array/"
---

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

Pass the array through a `Set` and spread it back into an array: `[...new Set(items)]`.

A `Set` stores each value once, so converting an array to a `Set` and back removes duplicates while keeping the first occurrence of each value in order.

```js
const ids = [3, 1, 3, 2, 1];
const unique = [...new Set(ids)]; // [3, 1, 2]
```

A `Set` compares objects by reference, so it will not merge two objects that merely look the same. To deduplicate objects by a field, keep one entry per key in a `Map`:

```js
const users = [
  { id: 1, name: "Ada" },
  { id: 2, name: "Linus" },
  { id: 1, name: "Ada (copy)" },
];

const uniqueUsers = [...new Map(users.map((u) => [u.id, u])).values()];
// later entries win; reverse the array first if you want the first one to win
```

## Related questions

- [How do you check if a key exists in a JavaScript object?](https://bugsee.com/answers/javascript/check-if-a-key-exists-in-an-object/)
