Skip to content

Commit 0f55e22

Browse files
committed
Adding Source Code and Questions of Set & Map
1 parent a4cdf89 commit 0f55e22

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,16 @@
3030
- [Recursion](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/blob/main/Recursion/README.md)
3131
- [Linear & Binary Search](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/blob/main/Searching%20Algorthims/README.md)
3232
- [Objects](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/blob/main/Objects/README.md)
33-
- [Sorting](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/blob/main/Sorting/README.md)
33+
- [Sorting](https://github.com/Vishal-raj-1/DSA-In-JS-With-Vishal/blob/main/Sorting/README.md)
34+
35+
## Upcoming Topics
36+
37+
- Linked List
38+
- Stack
39+
- Queue
40+
- Binary Tree
41+
- Binary Search Tree
42+
- Graph
43+
- Dynamic Programming
44+
- Miscellaneous
45+

Set & Map/README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy