-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSets.js
More file actions
83 lines (50 loc) · 1.54 KB
/
Sets.js
File metadata and controls
83 lines (50 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Sets in JavaScript Explained
// 1) What is a set?
// A set is a collection of unique values.
// They're ordered by how elements are inserted.
// But you can't index a set like an array.
// 2) How do you create a set?
// You need to call the Set function with the new keyword.
// You can pass in an iterable to the new Set() function
const mySet0 = new Set([1, 2, 3, 4, 4, 4, 4, 5])
console.log(mySet0)
// 3) How do sets work?
// Creating a set
const mySet = new Set()
// Adding elements to a set
mySet.add(1)
mySet.add(2)
mySet.add(3)
mySet.add(4)
console.log(mySet)
// Removing elements from a set
mySet.delete(1)
// Check if a value is in a set
console.log(mySet.has(1))
console.log(mySet.has(2))
// Getting the keys in a set
console.log(mySet.keys())
// Getting the values in a set
console.log(mySet.values())
// Getting the entries in a set
console.log(mySet.entries())
// Clearing a set
mySet.clear()
// 4) How to use iteration with sets?
// For loops
for (const value of mySet) {
console.log(value)
}
// Spreading sets
console.log([...mySet])
// Array.from() sets
console.log(Array.from(mySet))
// 5) Interview Question - Remove all duplicates elements of an Array
const arrayWithDuplicates = [1, 2, 3, 3, 3, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 8]
console.log(arrayWithDuplicates)
// Solutions
// const newSet = new Set(arrayWithDuplicates)
// console.log(newSet)
// const arrayWithoutDuplicates = Array.from(new Set(arrayWithDuplicates))
const arrayWithoutDuplicates = [...new Set(arrayWithDuplicates)]
console.log(arrayWithoutDuplicates)