Skip to content

Commit 5961185

Browse files
committed
chapter 06: [Sets]
1 parent 4ee46b1 commit 5961185

File tree

15 files changed

+1111
-2
lines changed

15 files changed

+1111
-2
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Work in Progress.
1515
* 03: [Stacks](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter03)
1616
* 04: [Queues and Deques](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter04)
1717
* 05: [LinkedLists](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter05)
18+
* 06: [Sets](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter06)
1819

1920
### Third Edition Updates
2021

@@ -40,6 +41,8 @@ Work in Progress.
4041

4142
* Or open the `examples/index.html` file to easily nagivate through all examples:
4243

44+
* Demo: [https://javascript-ds-algorithms-book.firebaseapp.com](https://javascript-ds-algorithms-book.firebaseapp.com)
45+
4346
<img src="examples/examples-screenshot.png">
4447

4548
Happy Coding!

examples/PacktDataStructuresAlgorithms.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/chapter06/01-Set.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
9+
<script src="01-Set.js"></script>
10+
</body>
11+
</html>

examples/chapter06/01-Set.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const { Set } = PacktDataStructuresAlgorithms;
2+
3+
const set = new Set();
4+
5+
set.add(1);
6+
console.log(set.values()); // outputs [1]
7+
console.log(set.has(1)); // outputs true
8+
console.log(set.size()); // outputs 1
9+
10+
set.add(2);
11+
console.log(set.values()); // outputs [1, 2]
12+
console.log(set.has(2)); // true
13+
console.log(set.size()); // 2
14+
15+
set.delete(1);
16+
console.log(set.values()); // outputs [2]
17+
18+
set.delete(2);
19+
console.log(set.values()); // outputs []
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
9+
<script src="02-SetOperations.js"></script>
10+
</body>
11+
</html>
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const { Set } = PacktDataStructuresAlgorithms;
2+
3+
// --------- Union ----------
4+
5+
let setA = new Set();
6+
setA.add(1);
7+
setA.add(2);
8+
setA.add(3);
9+
10+
let setB = new Set();
11+
setB.add(3);
12+
setB.add(4);
13+
setB.add(5);
14+
setB.add(6);
15+
16+
const unionAB = setA.union(setB);
17+
console.log(unionAB.values()); // [1, 2, 3, 4, 5, 6]
18+
19+
// --------- Intersection ----------
20+
21+
setA = new Set();
22+
setA.add(1);
23+
setA.add(2);
24+
setA.add(3);
25+
26+
setB = new Set();
27+
setB.add(2);
28+
setB.add(3);
29+
setB.add(4);
30+
31+
const intersectionAB = setA.intersection(setB);
32+
console.log(intersectionAB.values()); // [2, 3]
33+
34+
// --------- Difference ----------
35+
36+
setA = new Set();
37+
setA.add(1);
38+
setA.add(2);
39+
setA.add(3);
40+
41+
setB = new Set();
42+
setB.add(2);
43+
setB.add(3);
44+
setB.add(4);
45+
46+
const differenceAB = setA.difference(setB);
47+
console.log(differenceAB.values()); // [1]
48+
49+
// --------- Subset ----------
50+
51+
setA = new Set();
52+
setA.add(1);
53+
setA.add(2);
54+
55+
setB = new Set();
56+
setB.add(1);
57+
setB.add(2);
58+
setB.add(3);
59+
60+
const setC = new Set();
61+
setC.add(2);
62+
setC.add(3);
63+
setC.add(4);
64+
65+
console.log(setA.isSubsetOf(setB)); // true
66+
console.log(setA.isSubsetOf(setC)); // false

examples/chapter06/03-ES2015Set.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
9+
<script src="03-ES2015Set.js"></script>
10+
</body>
11+
</html>

examples/chapter06/03-ES2015Set.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const set = new Set();
2+
3+
set.add(1);
4+
console.log(set.values()); // outputs @Iterator
5+
console.log(set.has(1)); // outputs true
6+
console.log(set.size); // outputs 1
7+
8+
set.add(2);
9+
console.log(set.values()); // outputs [1, 2]
10+
console.log(set.has(2)); // true
11+
console.log(set.size); // 2
12+
13+
set.delete(1);
14+
console.log(set.values()); // outputs [2]
15+
16+
set.delete(2);
17+
console.log(set.values()); // outputs []
18+
19+
const setA = new Set();
20+
setA.add(1);
21+
setA.add(2);
22+
setA.add(3);
23+
24+
const setB = new Set();
25+
setB.add(2);
26+
setB.add(3);
27+
setB.add(4);
28+
29+
// --------- Union ----------
30+
const union = (set1, set2) => {
31+
const unionAb = new Set();
32+
set1.forEach(value => unionAb.add(value));
33+
set2.forEach(value => unionAb.add(value));
34+
return unionAb;
35+
};
36+
console.log(union(setA, setB));
37+
38+
// --------- Intersection ----------
39+
const intersection = (set1, set2) => {
40+
const intersectionSet = new Set();
41+
set1.forEach(value => {
42+
if (set2.has(value)) {
43+
intersectionSet.add(value);
44+
}
45+
});
46+
return intersectionSet;
47+
};
48+
console.log(intersection(setA, setB));
49+
50+
// alternative - works on FF only
51+
// console.log(new Set([x for (x of setA) if (setB.has(x))]));
52+
53+
// --------- Difference ----------
54+
const difference = (set1, set2) => {
55+
const differenceSet = new Set();
56+
set1.forEach(value => {
57+
if (!set2.has(value)) {
58+
differenceSet.add(value);
59+
}
60+
});
61+
return differenceSet;
62+
};
63+
console.log(difference(setA, setB));
64+
65+
// alternative - works on FF only
66+
// console.log(new Set([x for (x of setA) if (!setB.has(x))]));

examples/index.html

+9-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,15 @@
143143
</section>
144144
<section class="mdl-layout__tab-panel" id="scroll-tab-6">
145145
<div class="page-content">
146-
Soon.
146+
<div class="page-content mdl-layout--fixed-drawer">
147+
<div class="mdl-layout__drawer is-visible">
148+
<nav class="mdl-navigation">
149+
<a class="mdl-navigation__link" href="chapter06/01-Set.html">01-Set</a>
150+
<a class="mdl-navigation__link" href="chapter06/02-SetOperations.html">02-SetOperations</a>
151+
<a class="mdl-navigation__link" href="chapter06/03-ES2015Set.html">03-ES2015Set</a>
152+
</nav>
153+
</div>
154+
</div>
147155
</div>
148156
</section>
149157
<section class="mdl-layout__tab-panel" id="scroll-tab-7">

src/js/data-structures/set.js

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
export default class Set {
2+
constructor() {
3+
this.items = {};
4+
}
5+
add(value) {
6+
if (!this.has(value)) {
7+
this.items[value] = value;
8+
return true;
9+
}
10+
return false;
11+
}
12+
delete(value) {
13+
if (this.has(value)) {
14+
delete this.items[value];
15+
return true;
16+
}
17+
return false;
18+
}
19+
has(value) {
20+
return Object.prototype.hasOwnProperty.call(this.items, value);
21+
}
22+
values() {
23+
return Object.values(this.items);
24+
}
25+
union(otherSet) {
26+
const unionSet = new Set();
27+
this.values().forEach(value => unionSet.add(value));
28+
otherSet.values().forEach(value => unionSet.add(value));
29+
return unionSet;
30+
}
31+
intersection(otherSet) {
32+
const intersectionSet = new Set();
33+
const values = this.values();
34+
const otherValues = otherSet.values();
35+
let biggerSet = values;
36+
let smallerSet = otherValues;
37+
if (otherValues.length - values.length > 0) {
38+
biggerSet = otherValues;
39+
smallerSet = values;
40+
}
41+
smallerSet.forEach(value => {
42+
if (biggerSet.includes(value)) {
43+
intersectionSet.add(value);
44+
}
45+
});
46+
return intersectionSet;
47+
}
48+
difference(otherSet) {
49+
const differenceSet = new Set();
50+
this.values().forEach(value => {
51+
if (!otherSet.has(value)) {
52+
differenceSet.add(value);
53+
}
54+
});
55+
return differenceSet;
56+
}
57+
isSubsetOf(otherSet) {
58+
if (this.size() > otherSet.size()) {
59+
return false;
60+
}
61+
let isSubset = true;
62+
this.values().every(value => {
63+
if (!otherSet.has(value)) {
64+
isSubset = false;
65+
return false;
66+
}
67+
return true;
68+
});
69+
return isSubset;
70+
}
71+
isEmpty() {
72+
return this.size() === 0;
73+
}
74+
size() {
75+
return Object.keys(this.items).length;
76+
}
77+
clear() {
78+
this.items = {};
79+
}
80+
toString() {
81+
if (this.isEmpty()) {
82+
return '';
83+
}
84+
const values = this.values();
85+
let objString = `${values[0]}`;
86+
for (let i = 1; i < values.length; i++) {
87+
objString = `${objString},${values[i].toString()}`;
88+
}
89+
return objString;
90+
}
91+
}

src/js/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export { default as DoublyLinkedList } from './data-structures/doubly-linked-lis
1515
export { default as CircularLinkedList } from './data-structures/circular-linked-list';
1616
export { default as SortedLinkedList } from './data-structures/sorted-linked-list';
1717
export { default as StackLinkedList } from './data-structures/stack-linked-list';
18+
export { default as Set } from './data-structures/set';
1819

1920
export const util = _util;
2021

0 commit comments

Comments
 (0)