javascript / beginner
Snippet
Storing Unique Values with Set
A Set is a built-in object that only stores unique values. If you try to add a duplicate, it is automatically ignored.
snippet.js
1
2
3
const numbers = [1, 2, 2, 3];const uniqueNumbers = new Set(numbers);console.log(uniqueNumbers.size);
nodejs
Breakdown
1
new Set(numbers)
Creates a new Set from the array, filtering out the duplicate '2'.
2
uniqueNumbers.size
A property that returns the number of unique elements in the Set.