Unique values in array

OSCC · JavaScript, Array · Oct 22, 2020

Finds all unique values in an array.

  • Create a 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