HomeAbout Me

🔥 Mastering `Set` and `Map` in JavaScript

By Daniel Nguyen
Published in Javascript
April 06, 2024
1 min read

In modern JavaScript, two powerful built-in data structures are Set and Map. They are often underutilized by beginners, but mastering them can lead to cleaner and more efficient code — especially when solving algorithmic problems like those on LeetCode.


🔹 What is a Set?

A Set is a collection of unique values. This means no duplicate elements are allowed. It’s perfect for when you want to keep track of a group of distinct items.

✅ Key Characteristics:

  • No duplicate values
  • Iteration order is preserved
  • Useful for de-duplication and membership checks

✨ Common Methods:

const mySet = new Set();
mySet.add(1);
mySet.add(2);
mySet.add(2); // duplicate, will be ignored
console.log(mySet.has(2)); // true
console.log(mySet.size); // 2
mySet.delete(1);
mySet.clear(); // removes all elements

🔹 What is a Map?

A Map is a collection of key-value pairs, similar to an object, but with a few key differences:

  • Keys can be any type, not just strings or symbols
  • Maintains insertion order
  • More flexible and predictable for dynamic key-value storage

✨ Common Methods:

const myMap = new Map();
myMap.set('name', 'Alice');
myMap.set(42, 'The answer');
myMap.set(true, 'Yes');
console.log(myMap.get(42)); // "The answer"
console.log(myMap.has('name')); // true
myMap.delete(true);
myMap.clear(); // removes everything

💡 LeetCode Example: “Intersection of Two Arrays” (Easy)

Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique, and you may return the result in any order.

🔗 Problem: LeetCode 349 - Intersection of Two Arrays

🧠 Intuition:

We need to find unique common elements. This screams: use Set!

✅ JavaScript Solution:

var intersection = function(nums1, nums2) {
const set1 = new Set(nums1);
const result = new Set();
for (let num of nums2) {
if (set1.has(num)) {
result.add(num);
}
}
return [...result]; // convert Set back to Array
};

🧪 Example:

intersection([1, 2, 2, 1], [2, 2]); // Output: [2]
intersection([4, 9, 5], [9, 4, 9, 8, 4]); // Output: [4, 9]

🆚 When to Use Set vs Map

Use CaseUse SetUse Map
Unique values only✅ Yes🚫 Not suitable
Key-value pairs🚫 Not applicable✅ Yes
Fast membership check✅ O(1) lookup✅ O(1) key lookup
Store extra info per item🚫 No✅ Yes (value per key)

🧵 Conclusion

Both Set and Map offer powerful capabilities that are often more efficient and expressive than arrays or plain objects, especially for algorithmic problems.

✅ Use Set when you need uniqueness or fast existence checks. ✅ Use Map when you need to associate values with keys (like a hash table).


💬 Now it’s your turn! Try using Set and Map in your next LeetCode challenge — you’ll be surprised how much cleaner your code becomes.


Tags

#Javascript

Share

Previous Article
JI: Implementing a document versioning system

Table Of Contents

1
🔹 What is a Set?
2
🔹 What is a Map?
3
💡 LeetCode Example: "Intersection of Two Arrays" (Easy)
4
🆚 When to Use Set vs Map
5
🧵 Conclusion

Related Posts

Debounce in JavaScript
July 15, 2025
1 min
© 2025, All Rights Reserved.
Powered By

Quick Links

About Me

Legal Stuff

Social Media