uniqueElements

JavaScript, Array

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)];
Examples
uniqueElements([1, 2, 2, 3, 4, 4, 5]); // [1, 2, 3, 4, 5]

Recommended snippets & collections

  • JavaScript Array Tricks

    Snippet collection

    Learn a handful of awesome tips and tricks that you can leverage in your code to make array manipulation a breeze.

  • uniqueElementsBy

    JavaScript, Array

    Finds all unique values of an array, based on a provided comparator function.

  • uniqueElementsByRight

    JavaScript, Array

    Finds all unique values of an array, based on a provided comparator function, starting from the right.

  • frequencies

    JavaScript, Array

    Creates an object with the unique values of an array as keys and their frequencies as the values.