◐ Shell
reader mode source ↗
EN

We want to make this open-source project available for people all around the world.

Help to translate the content of this tutorial to your language!

Search
Search
back to the lesson

Filter unique array members

importance: 5

Let arr be an array.

Create a function unique(arr) that should return an array with unique items of arr.

For instance:

function unique(arr) {
  /* your code */
}

let values = ["Hare", "Krishna", "Hare", "Krishna",
  "Krishna", "Krishna", "Hare", "Hare", ":-O"
];

alert( unique(values) ); // Hare, Krishna, :-O

P.S. Here strings are used, but can be values of any type.

P.P.S. Use Set to store unique values.

Open a sandbox with tests.

solution
function unique(arr) {
  return Array.from(new Set(arr));
}

Open the solution with tests in a sandbox.