Set Data Structure in Javascript

A Set is a built-in data structure in JavaScript that stores unique values of any type. Unlike arrays, Sets automatically prevent duplicate values and don't maintain insertion order for iteration purposes, making them ideal for storing collections of unique items.

Creating a Set

You can create a Set using the Set constructor:

// Empty Set
let emptySet = new Set();
console.log(emptySet);

// Set with initial values
let numbers = new Set([1, 2, 3, 4, 5]);
console.log(numbers);

// Set automatically removes duplicates
let duplicates = new Set([1, 2, 2, 3, 3, 4]);
console.log(duplicates);
Set(0) {}
Set(5) { 1, 2, 3, 4, 5 }
Set(4) { 1, 2, 3, 4 }

Basic Set Operations

Adding Values

let fruits = new Set();

fruits.add("apple");
fruits.add("banana");
fruits.add("apple"); // Duplicate - won't be added

console.log(fruits);
console.log("Size:", fruits.size);
Set(2) { 'apple', 'banana' }
Size: 2

Checking if Value Exists

let colors = new Set(["red", "green", "blue"]);

console.log(colors.has("red"));    // true
console.log(colors.has("yellow")); // false
true
false

Deleting Values

let animals = new Set(["cat", "dog", "bird"]);

console.log("Before deletion:", animals);
animals.delete("dog");
console.log("After deletion:", animals);

// Clear all values
animals.clear();
console.log("After clear:", animals);
Before deletion: Set(3) { 'cat', 'dog', 'bird' }
After deletion: Set(2) { 'cat', 'bird' }
After clear: Set(0) {}

Iterating Through a Set

let languages = new Set(["JavaScript", "Python", "Java"]);

// Using for...of loop
console.log("Using for...of:");
for (let language of languages) {
    console.log(language);
}

// Using forEach
console.log("\nUsing forEach:");
languages.forEach(language => {
    console.log(language);
});
Using for...of:
JavaScript
Python
Java

Using forEach:
JavaScript
Python
Java

Practical Use Cases

Removing Duplicates from Array

let numbersWithDuplicates = [1, 2, 2, 3, 4, 4, 5];
let uniqueNumbers = [...new Set(numbersWithDuplicates)];

console.log("Original:", numbersWithDuplicates);
console.log("Unique:", uniqueNumbers);
Original: [ 1, 2, 2, 3, 4, 4, 5 ]
Unique: [ 1, 2, 3, 4, 5 ]

Set Operations

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

// Union
let union = new Set([...setA, ...setB]);
console.log("Union:", union);

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

// Difference
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 }

Set vs Array Comparison

Feature Set Array
Duplicates Not allowed Allowed
Access by Index No Yes
Check if value exists O(1) with has() O(n) with includes()
Insertion order Maintained Maintained

Conclusion

JavaScript Sets are perfect for storing unique values and performing membership tests efficiently. Use Sets when you need to ensure uniqueness or perform fast lookups, and convert to arrays when you need indexed access.

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

573 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements