-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhashSet-use-object.js
197 lines (167 loc) · 3.66 KB
/
hashSet-use-object.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
Leetcode
705 Design a HashSet without using any built-in hash table libraries.
easy
To be specific, your design should include these functions:
add(value): Insert a value into the HashSet.
contains(value): Return whether the value exists in the HashSet or not.
remove(value): Remove a value in the HashSet. If the value does not exist in
the HashSet, do nothing.
Example:
MyHashSet hashSet = new MyHashSet();
hashSet.add(1);
hashSet.add(2);
hashSet.contains(1); // returns true
hashSet.contains(3); // returns false (not found)
hashSet.add(2);
hashSet.contains(2); // returns true
hashSet.remove(2);
hashSet.contains(2); // returns false (already removed)
Note:
All values will be in the range of [0, 1000000].
The number of operations will be in the range of [1, 10000].
Please do not use the built-in HashSet library.
*/
/*
Approach use obj
this solution under requirement, because {} is using any built-in hash in JS
*/
class HashSet {
constructor() {
this.data = {};
this.length = 0;
}
/**
* @param {number} key
* @return {void}
*/
add(key) {
//debugger
// count only unique values
if (!this.contains(key)) this.length++;
key = key.toString();
this.data[key] = key;
}
/**
* Returns true if this set contains the specified element
* @param {number} key
* @return {boolean}
*/
contains(key) {
key = key.toString()
// Converts Object to boolean. If it was false (e.g. 0, null, undefined, etc.),
// it will be false, otherwise, true.
return (!!this.data[key] && this.data.hasOwnProperty(key));
}
/**
* @param {number} key
* @return {void}
*/
remove(key) {
if (!this.contains(key)) {
return
} else {
delete this.data[key.toString()];
this.length--;
}
}
// remove with return
// remove(value) {
// value = value.toString();
// if (!this.data.contains(value)) {
// return false
// } else {
// delete this.data[value];
// this.length--;
// return true;
// }
// }
size() {
return this.length
}
isEmpty() {
return this.length === 0
}
toArray() {
}
}
/*
Approach use obj, simple API
*/
class HashSetVariant1 {
constructor() {
this.data = {};
this.length = 0;
}
add(key) {
if (!this.data[key]) {
this.data[key] = true;
this.length++;
}
}
remove(key) {
if (this.data[key]) {
delete this.data[key];
this.length--;
}
}
contains(key) {
if (!this.data[key]) {
return false;
}
else return true;
}
size() {
return this.length;
}
isEmpty() {
return this.length === 0;
}
}
// tests
const hash = new HashSet();
hash.add(1);
hash.add(2);
hash.contains(1);
hash.remove(2);
hash.remove(3);
hash.contains(3);
hash.add(1);
hash.add(2);
//console.log('hash', hash)
// https://leetcode.com/problems/design-hashset/discuss/768659/Python-Easy-Multiplicative-Hash-explained
// return ((key*1031237) & (1<<20) - 1)>>5
// key is val
// let mySet = new Set()
// mySet.add(10000)
// hashing with load factor
// array https://leetcode.com/problems/design-hashset/discuss/137257/javascript-solution%3A-100ms.
// https://leetcode.com/problems/design-hashset/discuss/304242/Javascript-Solution-Easy-To-Understand
// https://javascript.info/class-inheritance
// interface js
// strategy pattern
/*
class Duck {
constructor() {
this.flyBehaviour = new Date()
}
}
k = new Duck()
class CanFly {
fly = () => console.log('fly...')
}
class FlyingDuck extends Duck {
constructor() {
super();
this.newFly = new CanFly();
}
}
pp = new CanFly()
pp = new FlyingDuck()
pp.flyBehaviour
pp.newFly.fly()
*/
export {
HashSet,
HashSetVariant1,
}