|
| 1 | +# Map, WeakMap, Set, WeakSet in JavaScript |
| 2 | + |
| 3 | +## Set in JavaScript: |
| 4 | + |
| 5 | +```javascript |
| 6 | +// Creating a Set |
| 7 | +const mySet = new Set(); |
| 8 | + |
| 9 | +// Adding values to the Set |
| 10 | +mySet.add(1); |
| 11 | +mySet.add("hello"); |
| 12 | +mySet.add(true); |
| 13 | + |
| 14 | +// Checking if a value exists |
| 15 | +console.log(mySet.has(1)); // true |
| 16 | + |
| 17 | +// Removing a value |
| 18 | +mySet.delete("hello"); |
| 19 | + |
| 20 | +// Iterating through the Set |
| 21 | +for (const value of mySet) { |
| 22 | + console.log(value); |
| 23 | +} |
| 24 | + |
| 25 | +// Size of the Set |
| 26 | +console.log(mySet.size); // 2 |
| 27 | + |
| 28 | +// Clearing the Set |
| 29 | +mySet.clear(); |
| 30 | +``` |
| 31 | + |
| 32 | +## Map in JavaScript |
| 33 | + |
| 34 | +```javascript |
| 35 | +// Creating a Map |
| 36 | +const myMap = new Map(); |
| 37 | + |
| 38 | +// Adding key-value pairs to the Map |
| 39 | +myMap.set("name", "John"); |
| 40 | +myMap.set("age", 30); |
| 41 | + |
| 42 | +// Getting a value using a key |
| 43 | +console.log(myMap.get("name")); // John |
| 44 | + |
| 45 | +// Checking if a key exists |
| 46 | +console.log(myMap.has("age")); // true |
| 47 | + |
| 48 | +// Removing a key-value pair |
| 49 | +myMap.delete("age"); |
| 50 | + |
| 51 | +// Iterating through the Map |
| 52 | +for (const [key, value] of myMap) { |
| 53 | + console.log(key, value); |
| 54 | +} |
| 55 | + |
| 56 | +// Size of the Map |
| 57 | +console.log(myMap.size); // 1 |
| 58 | + |
| 59 | +// Clearing the Map |
| 60 | +myMap.clear(); |
| 61 | +``` |
| 62 | + |
| 63 | +## Weak Map in JavaScript |
| 64 | + |
| 65 | +```javascript |
| 66 | +let obj = { key: 'value' }; |
| 67 | + |
| 68 | +// Creating a WeakMap |
| 69 | +let weakMap = new WeakMap(); |
| 70 | +weakMap.set(obj, 'metadata'); |
| 71 | + |
| 72 | +// Checking if the object still exists in the WeakMap |
| 73 | +console.log(weakMap.has(obj)); // true |
| 74 | + |
| 75 | +// Removing the strong reference to the object |
| 76 | +obj = null; |
| 77 | + |
| 78 | +// At this point, the object is no longer strongly referenced |
| 79 | +// The WeakMap's weak reference will allow the object to be garbage collected |
| 80 | +console.log(weakMap.has(obj)); // false |
| 81 | +``` |
| 82 | + |
| 83 | +## Practice Questions: |
| 84 | + |
| 85 | +1. [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) |
| 86 | +2. [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) (LeetCode 349) |
| 87 | +3. [Distribute Candies](https://leetcode.com/problems/distribute-candies/) |
| 88 | +4. [Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/) (LeetCode 128) |
| 89 | +5. [Happy Number](https://leetcode.com/problems/happy-number/) |
| 90 | +6. [First Unique Character In A String](https://leetcode.com/problems/first-unique-character-in-a-string/) |
| 91 | +7. [Find Common Characters](https://leetcode.com/problems/find-common-characters/) |
| 92 | +8. [Sort Characters By Frequency](https://leetcode.com/problems/sort-characters-by-frequency/) |
| 93 | +9. [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) |
| 94 | +10. [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) |
| 95 | +11. [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) |
0 commit comments