You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Set stores unique values of any type (primitives or object references) and preserves insertion order. It is ideal for deduplication and set operations like union, intersection, and difference.
10
+
Sets store unique values of any type and are ideal for deduplication and membership checks.
11
+
12
+
Sets can contain mixed types (numbers, strings, objects).
13
+
14
+
Sets are dynamic—no fixed size needed.
15
+
16
+
Sets preserve insertion order when iterating.
17
+
18
+
Sets are iterable and work with `for...of` and `forEach`.
19
+
20
+
Set vs Array
21
+
- Arrays allow duplicates; Sets do not.
22
+
- Arrays use index access; Sets use membership checks and iteration.
23
+
- Typical membership tests and deletes in Sets are O(1).
11
24
12
-
```js
13
-
consts=newSet([1, 2, 2, 3]);
14
-
console.log(s.size); // 3
15
-
```
16
25
17
26
## Creating Sets
18
27
19
28
```js
20
-
constempty=newSet();
21
-
constfromArray=newSet([1, 2, 3]);
22
-
constfromMixed=newSet([1, '1', { a:1 }]); // all distinct
29
+
constset=newSet([1, 2, 3]);
30
+
constmixed=newSet([1, '1', { a:1 }]);
31
+
for (constitemof set) console.log(item);
23
32
```
24
33
25
-
## Core Operations
34
+
Result:
35
+
```
36
+
1
37
+
2
38
+
3
39
+
```
40
+
41
+
## Add & Remove
26
42
27
43
```js
28
-
consts=newSet();
29
-
s.add(1); // add value
30
-
s.add(2);
31
-
s.has(1); // true
32
-
s.delete(2); // remove value
33
-
// s.clear(); // remove all
44
+
set.add(4);
45
+
set.add(4);
34
46
```
35
47
36
-
Note: object equality is by reference; two identical objects are different entries.
48
+
Result:
49
+
```
50
+
1
51
+
2
52
+
3
53
+
4
54
+
```
37
55
38
-
```js
39
-
consta= { x:1 }, b = { x:1 };
40
-
consts=newSet([a, b]);
41
-
console.log(s.size); // 2
56
+
Check membership:
57
+
58
+
```
59
+
console.log(set.has(4));
60
+
```
61
+
62
+
Result:
63
+
```
64
+
true
42
65
```
43
66
44
-
## Iteration
67
+
Remove values:
45
68
46
-
```js
47
-
consts=newSet(['a', 'b']);
48
-
for (constvof s) console.log(v);
69
+
```
70
+
set.delete(4);
71
+
```
72
+
73
+
Result:
49
74
50
-
// or
51
-
s.forEach((v) =>console.log(v));
75
+
```
76
+
1
77
+
2
78
+
3
79
+
```
80
+
81
+
Size:
82
+
83
+
```
84
+
console.log(set.size);
85
+
```
86
+
87
+
Result:
88
+
```
89
+
3
90
+
```
91
+
92
+
Clear:
93
+
94
+
```
95
+
set.clear();
96
+
```
97
+
98
+
Result:
99
+
```
100
+
console.log(set.size);
101
+
0
52
102
```
53
103
54
104
## Conversions
@@ -58,33 +108,40 @@ Array ↔ Set:
58
108
```js
59
109
constarr= [1, 2, 2, 3];
60
110
consts=newSet(arr);
61
-
constbackToArray= [...s];// [1, 2, 3]
111
+
constback= [...s];
62
112
```
63
113
64
-
Dedupe with Set:
114
+
Result:
115
+
```
116
+
[1, 2, 3]
117
+
```
118
+
119
+
Deduplicate arrays:
65
120
66
121
```js
67
-
constunique= (xs) => [...newSet(xs)];
68
-
unique([1, 1, 2, 3, 2]); // [1,2,3]
122
+
constunique=xs=> [...newSet(xs)];
123
+
unique([1, 1, 2, 3, 2]);
124
+
```
125
+
126
+
Result:
127
+
```
128
+
[1, 2, 3]
69
129
```
70
130
71
-
## Set Algebra Helpers
131
+
## Set Algebra
72
132
73
133
```js
74
134
constunion= (A, B) =>newSet([...A, ...B]);
75
-
76
135
constintersection= (A, B) => {
77
136
constout=newSet();
78
137
for (constvofA) if (B.has(v)) out.add(v);
79
138
return out;
80
139
};
81
-
82
140
constdifference= (A, B) => {
83
141
constout=newSet();
84
142
for (constvofA) if (!B.has(v)) out.add(v);
85
143
return out;
86
144
};
87
-
88
145
constisSubset= (A, B) => {
89
146
for (constvofA) if (!B.has(v)) returnfalse;
90
147
returntrue;
@@ -96,25 +153,30 @@ Examples:
96
153
```js
97
154
constA=newSet([1, 2, 3]);
98
155
constB=newSet([2, 3, 4]);
99
-
100
-
[...union(A, B)]; // [1,2,3,4]
101
-
[...intersection(A, B)]; // [2,3]
102
-
[...difference(A, B)]; // [1]
103
-
isSubset(newSet([2,3]), A); // true
156
+
[...union(A, B)];
157
+
[...intersection(A, B)];
158
+
[...difference(A, B)];
159
+
isSubset(newSet([2, 3]), A);
104
160
```
105
161
106
-
## Performance (Typical)
162
+
Result:
163
+
```
164
+
[1, 2, 3, 4]
165
+
[2, 3]
166
+
[1]
167
+
true
168
+
```
107
169
108
-
| Operation | Complexity |
109
-
| ----------- | ---------- |
110
-
| add/delete | O(1) |
111
-
| has | O(1) |
112
-
| size | O(1) |
113
-
| iterate | O(n) |
170
+
## Big‑O Complexity
114
171
115
-
## Pitfalls
172
+
| Operation | Complexity |
173
+
| ------------------- | ---------- |
174
+
| has / add / delete | O(1) |
175
+
| clear / size | O(1) |
176
+
| iterate | O(n) |
116
177
117
-
- Equality is by reference for objects; identical shape ≠ same entry
118
-
- No index access like arrays; use iteration
119
-
- Converting large sets repeatedly can be costly; cache results if needed
178
+
## Notes & Pitfalls
120
179
180
+
- Object equality is by reference
181
+
- No index access—iterate or convert to array
182
+
- Cache conversions for large sets to avoid repeated cost
0 commit comments