-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathDesign HashSet.js
39 lines (28 loc) · 908 Bytes
/
Design HashSet.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
var MyHashSet = function() {
// Really you should just
// Make your own object, but instead
// we have attached ourself to the
// `this` object which then becomes our hashmap.
// What you should instead do is this:
// this.hash_map = {}
// And then update our following functions
};
MyHashSet.prototype.add = function(key) {
// Constant Time
// Linear Space | To the size of the input key
// You can access objects using array notation
this[key] = null;
};
MyHashSet.prototype.remove = function(key) {
// Constant Time
// Constant Space
// You can access objects using array notation
// Here we use the delete keyword.
delete this[key]
};
MyHashSet.prototype.contains = function(key) {
// Constant Time
// Constant Space
// This just asks if the property exists
return this.hasOwnProperty(key)
};