Design HashSet LeetCode solution in JavaScript

1
var MyHashSet = function () {
2
// Really you should just
3
// Make your own object, but instead
4
// we have attached ourself to the
5
// `this` object which then becomes our hashmap.
6
// What you should instead do is this:
7
// this.hash_map = {}
8
// And then update our following functions
9
};
10

11
MyHashSet.prototype.add = function (key) {
12
// Constant Time
13
// Linear Space | To the size of the input key
14
// You can access objects using array notation
15

16
this[key] = null;
17
};
18

19
MyHashSet.prototype.remove = function (key) {
20
// Constant Time
21
// Constant Space
22
// You can access objects using array notation
23
// Here we use the delete keyword.
24

25
delete this[key];
26
};
27

28
MyHashSet.prototype.contains = function (key) {
29
// Constant Time
30
// Constant Space
31
// This just asks if the property exists
32

33
return this.hasOwnProperty(key);
34
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0