Files
30-seconds-of-code/snippets/uniqueElements.md
Isabelle Viktoria Maciohsek aa425812b4 Update snippet descriptions
2020-10-22 20:24:44 +03:00

18 lines
359 B
Markdown

---
title: uniqueElements
tags: array,beginner
---
Finds all unique values in an array.
- Create a `new Set()` from the given array to discard duplicated values.
- Use the spread operator (`...`) to convert it back to an array.
```js
const uniqueElements = arr => [...new Set(arr)];
```
```js
uniqueElements([1, 2, 2, 3, 4, 4, 5]); // [1, 2, 3, 4, 5]
```