Removing duplicates from an array using Set
Using Set
collections, you can
easily remove duplicates from arrays. To
do this, the array must be converted into
a Set
collection. Since this
collection cannot contain duplicate
elements, they will disappear during
the conversion. If we then convert
the collection back to an array, then
we get the array without duplicates.
Let's look at an example. Let's say we have the following array with duplicates:
let arr = [1, 2, 3, 1, 3, 4];
Let's create a Set
collection based on it:
let set = new Set(arr);
Now let's convert our collection back to an array:
let arr = [1, 2, 3, 1, 3, 4];
let res = [...new Set(arr)];
console.log(res); // shows [1, 2, 3, 4]
Write a function that takes an array as a parameter and returns this array without duplicates.