It is possible to fill a Set
collection with some values during
creation by passing an array with
data as a parameter:
let set = new Set([1, 2, 3]);
Moreover, if there are duplicates in the array, then they will not appear in the collection:
let set = new Set([1, 2, 3, 3, 4, 4, 5]);
console.log(set); // shows [1, 2, 3, 4, 5]
Given an array of numbers:
let arr = [1, 2, 3, 1, 3, 4];
Using this array create a
Set
collection.
Create a Set
collection with
initial values 1
, 2
and 3
. Use the add
method to add another number 2
to the collection. Output the contents
of the collection to the console,
make sure that the number 2
has not been added repeatedly.