Skip to content

Commit 945f11f

Browse files
Refactor Set note formatting to align with Arrays: sections, results, conversions, algebra, Big‑O
1 parent fa60dfd commit 945f11f

File tree

1 file changed

+112
-50
lines changed

1 file changed

+112
-50
lines changed

src/content/notes/data-structure-set-in-javascript.md

Lines changed: 112 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,98 @@ tags: 'js-data-structure'
77

88
## Overview
99

10-
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).
1124

12-
```js
13-
const s = new Set([1, 2, 2, 3]);
14-
console.log(s.size); // 3
15-
```
1625

1726
## Creating Sets
1827

1928
```js
20-
const empty = new Set();
21-
const fromArray = new Set([1, 2, 3]);
22-
const fromMixed = new Set([1, '1', { a: 1 }]); // all distinct
29+
const set = new Set([1, 2, 3]);
30+
const mixed = new Set([1, '1', { a: 1 }]);
31+
for (const item of set) console.log(item);
2332
```
2433

25-
## Core Operations
34+
Result:
35+
```
36+
1
37+
2
38+
3
39+
```
40+
41+
## Add & Remove
2642

2743
```js
28-
const s = new Set();
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);
3446
```
3547

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+
```
3755

38-
```js
39-
const a = { x: 1 }, b = { x: 1 };
40-
const s = new Set([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
4265
```
4366

44-
## Iteration
67+
Remove values:
4568

46-
```js
47-
const s = new Set(['a', 'b']);
48-
for (const v of s) console.log(v);
69+
```
70+
set.delete(4);
71+
```
72+
73+
Result:
4974

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
52102
```
53103

54104
## Conversions
@@ -58,33 +108,40 @@ Array ↔ Set:
58108
```js
59109
const arr = [1, 2, 2, 3];
60110
const s = new Set(arr);
61-
const backToArray = [...s]; // [1, 2, 3]
111+
const back = [...s];
62112
```
63113

64-
Dedupe with Set:
114+
Result:
115+
```
116+
[1, 2, 3]
117+
```
118+
119+
Deduplicate arrays:
65120

66121
```js
67-
const unique = (xs) => [...new Set(xs)];
68-
unique([1, 1, 2, 3, 2]); // [1,2,3]
122+
const unique = xs => [...new Set(xs)];
123+
unique([1, 1, 2, 3, 2]);
124+
```
125+
126+
Result:
127+
```
128+
[1, 2, 3]
69129
```
70130

71-
## Set Algebra Helpers
131+
## Set Algebra
72132

73133
```js
74134
const union = (A, B) => new Set([...A, ...B]);
75-
76135
const intersection = (A, B) => {
77136
const out = new Set();
78137
for (const v of A) if (B.has(v)) out.add(v);
79138
return out;
80139
};
81-
82140
const difference = (A, B) => {
83141
const out = new Set();
84142
for (const v of A) if (!B.has(v)) out.add(v);
85143
return out;
86144
};
87-
88145
const isSubset = (A, B) => {
89146
for (const v of A) if (!B.has(v)) return false;
90147
return true;
@@ -96,25 +153,30 @@ Examples:
96153
```js
97154
const A = new Set([1, 2, 3]);
98155
const B = new Set([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(new Set([2,3]), A); // true
156+
[...union(A, B)];
157+
[...intersection(A, B)];
158+
[...difference(A, B)];
159+
isSubset(new Set([2, 3]), A);
104160
```
105161

106-
## Performance (Typical)
162+
Result:
163+
```
164+
[1, 2, 3, 4]
165+
[2, 3]
166+
[1]
167+
true
168+
```
107169

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
114171

115-
## Pitfalls
172+
| Operation | Complexity |
173+
| ------------------- | ---------- |
174+
| has / add / delete | O(1) |
175+
| clear / size | O(1) |
176+
| iterate | O(n) |
116177

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
120179

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

Comments
 (0)