Junior — Middle
What is the main function of the Set collection in programming?
sobes.tech AI
Answer from AI
The Set collection in programming is designed to store unique values without a specific order. The main function of Set is to ensure storage of elements without duplicates, which is useful when you need to quickly check for the presence of an element or obtain a set of unique values.
Example in JavaScript (Node.js):
const set = new Set();
set.add(1);
set.add(2);
set.add(2); // will not be added again, since 2 already exists
console.log(set.has(2)); // true
console.log(set.size); // 2
Thus, Set helps to work efficiently with unique elements.