The Set Class in Javascript

JavaScript's built-in Set class is a collection of unique values that allows you to store distinct elements of any type. Unlike arrays, Sets automatically prevent duplicate values and provide efficient methods for adding, removing, and checking element existence.

Creating a Set

You can create a Set using the new Set() constructor, optionally passing an iterable like an array:

// Empty set
let mySet = new Set();

// Set from array
let numbersSet = new Set([1, 2, 3, 4, 4, 5]);
console.log(numbersSet);

// Duplicates are automatically removed
let wordsSet = new Set(['hello', 'world', 'hello']);
console.log(wordsSet);
Set(5) { 1, 2, 3, 4, 5 }
Set(2) { 'hello', 'world' }

Basic Set Methods

The Set class provides several essential methods for managing unique collections:

let fruits = new Set();

// Adding elements
fruits.add('apple');
fruits.add('banana');
fruits.add('orange');
fruits.add('apple'); // Duplicate ignored

console.log('Size:', fruits.size);
console.log('Has banana:', fruits.has('banana'));
console.log('Has grape:', fruits.has('grape'));

// Deleting elements
fruits.delete('banana');
console.log('After deleting banana:', fruits);

// Iterating through Set
console.log('Remaining fruits:');
fruits.forEach(fruit => console.log('-', fruit));
Size: 3
Has banana: true
Has grape: false
After deleting banana: Set(2) { 'apple', 'orange' }
Remaining fruits:
- apple
- orange

Set Operations

Sets are useful for mathematical operations like union, intersection, and difference:

let setA = new Set([1, 2, 3, 4]);
let setB = new Set([3, 4, 5, 6]);

// Union: all unique elements from both sets
let union = new Set([...setA, ...setB]);
console.log('Union:', union);

// Intersection: common elements
let intersection = new Set([...setA].filter(x => setB.has(x)));
console.log('Intersection:', intersection);

// Difference: elements in setA but not in setB
let difference = new Set([...setA].filter(x => !setB.has(x)));
console.log('Difference:', difference);
Union: Set(6) { 1, 2, 3, 4, 5, 6 }
Intersection: Set(2) { 3, 4 }
Difference: Set(2) { 1, 2 }

Converting Between Arrays and Sets

Sets work seamlessly with arrays using the spread operator:

// Remove duplicates from array
let numbers = [1, 2, 2, 3, 3, 4, 5, 5];
let uniqueNumbers = [...new Set(numbers)];
console.log('Original:', numbers);
console.log('Unique:', uniqueNumbers);

// Clear all elements
let testSet = new Set([1, 2, 3]);
console.log('Before clear:', testSet);
testSet.clear();
console.log('After clear:', testSet);
Original: [ 1, 2, 2, 3, 3, 4, 5, 5 ]
Unique: [ 1, 2, 3, 4, 5 ]
Before clear: Set(3) { 1, 2, 3 }
After clear: Set(0) {}

Comparison with Arrays

Feature Set Array
Duplicates Not allowed Allowed
Element lookup O(1) with has() O(n) with includes()
Indexed access No Yes
Ordered Insertion order Index order

Conclusion

JavaScript Sets provide an efficient way to store unique values with fast lookup operations. Use Sets when you need to maintain distinct elements or perform set-based operations like finding unique values in arrays.

Updated on: 2026-03-15T23:18:59+05:30

186 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements