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.
const uniqueElements = arr => [...new Set(arr)];
uniqueElements([1, 2, 2, 3, 4, 4, 5]); // [1, 2, 3, 4, 5]
Recommended snippets & collections
- Snippet collection
Learn a handful of awesome tips and tricks that you can leverage in your code to make array manipulation a breeze.
Finds all unique values of an array, based on a provided comparator function.
Finds all unique values of an array, based on a provided comparator function, starting from the right.
Creates an object with the unique values of an array as keys and their frequencies as the values.