HomeAbout Me

🔥 Mastering `Set` in JavaScript: The Underrated Power Tool for Unique Data

By Daniel Nguyen
Published in Javascript
April 06, 2025
1 min read
🔥 Mastering `Set` in JavaScript: The Underrated Power Tool for Unique Data

When dealing with arrays in JavaScript, one of the most common problems is duplicate values. Whether you’re cleaning up form inputs, filtering search results, or writing algorithms, you often need to handle uniqueness.

That’s where JavaScript’s built-in Set comes in — a simple but powerful tool you might not be using enough.


🧠 What Is a Set?

A Set is a collection of values where each value must be unique. It’s similar to an array, but with no duplicates allowed.

✅ Key Features:

  • Stores only unique values
  • Maintains insertion order
  • Supports fast lookups (O(1) average)
  • Can hold any type (numbers, strings, objects, etc.)

✍️ Basic Usage

const fruits = new Set();
fruits.add("apple");
fruits.add("banana");
fruits.add("apple"); // duplicate — will be ignored
console.log(fruits); // Set { 'apple', 'banana' }

🔁 Loop through a Set

You can use a for...of loop to iterate through the values in a Set:

for (const fruit of fruits) {
console.log(fruit);
}
// Output:
// apple
// banana

This is super helpful when you want to process each unique item in a Set.


🔍 Common Methods

MethodDescription
add(value)Adds a value
delete(value)Removes a value
has(value)Checks if a value exists
clear()Removes all values
sizeNumber of elements in the Set

🧪 Real-World Example: Remove Duplicates from an Array

const numbers = [1, 2, 2, 3, 4, 4, 5];
const uniqueNumbers = [...new Set(numbers)];
console.log(uniqueNumbers); // [1, 2, 3, 4, 5]

One-liner to deduplicate an array. Clean. Fast. Beautiful. 😎


⚡ Use Case in Coding Interviews: LeetCode #217

Given an array of numbers, return true if it contains any duplicates.

function containsDuplicate(nums) {
const seen = new Set();
for (const num of nums) {
if (seen.has(num)) return true;
seen.add(num);
}
return false;
}

This is faster and cleaner than sorting or nested loops. The power of Set!


🧰 Bonus: Convert Between Set and Array

const mySet = new Set([1, 2, 3]);
const myArray = [...mySet]; // [1, 2, 3]

You can go back and forth easily — making Set a great helper in array operations.


🚫 Things to Keep in Mind

  • Objects and arrays are compared by reference, not value:

    const set = new Set();
    set.add({ name: "Alice" });
    set.add({ name: "Alice" });
    console.log(set.size); // 2 (because they are different objects)
  • No direct way to get an item by index (like arrays)


🎯 When to Use Set

Use a Set when you:

  • Want to ensure uniqueness
  • Need fast existence checks
  • Want to deduplicate an array
  • Are solving problems involving membership or frequency

🔚 Final Thoughts

The Set object is often overlooked but incredibly useful. It can clean up your code, improve performance, and help you ace technical interviews. Next time you think about removing duplicates or checking existence, remember: Set has your back.


📌 Pro tip: Pair Set with Map, and you’ll be ready to tackle even more complex data problems in JavaScript!


Tags

#Javascript

Share

Previous Article
📘 Section 30: Examining Benefit Measurement Methods

Table Of Contents

1
🧠 What Is a Set?
2
✍️ Basic Usage
3
🔍 Common Methods
4
🧪 Real-World Example: Remove Duplicates from an Array
5
⚡ Use Case in Coding Interviews: LeetCode #217
6
🧰 Bonus: Convert Between Set and Array
7
🚫 Things to Keep in Mind
8
🎯 When to Use Set
9
🔚 Final Thoughts

Related Posts

How `this` behaves in JavaScript
April 24, 2025
1 min
© 2025, All Rights Reserved.
Powered By

Quick Links

About Me

Legal Stuff

Social Media