forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashSet.js
More file actions
107 lines (96 loc) · 3.04 KB
/
Copy pathHashSet.js
File metadata and controls
107 lines (96 loc) · 3.04 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Time Complexity : O(1)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No
/*
* Approach - Double Hashing
* Since the constraint was 0 - 10^6 we use the sqrt to split buckets to avoid huge memory wastage
* We use double hashing to avoid collision
* Add:
* - We get the first hash value by using key % buckets
* - We get the second hash value by key / bucketItems
* - if the array of the fist hash is empty or null we initialize it to a 2D
* array with the bucketItems length and defaulting it to false.
* - if it's not then we do storage[bucket][bucketItem] and set the value to true
*
* Remove:
* - We get the first hash value by using key % buckets
* - We get the second hash value by key / bucketItems
* - if the array of the fist hash is empty or null we return
* - if it's not then we do storage[bucket][bucketItem] = false since we are removing the value
*
* Contains:
* - We get the first hash value by using key % buckets
* - We get the second hash value by key / bucketItems
* - if the array of the fist hash is empty or null we return false
* - if it's not then we return storage[bucket][bucketItem]
* which would be true / false depending on if the value exists
*
* */
const MyHashSet = function() {
this.buckets = 1000;
this.bucketItems = 1000;
this.storage = new Array(this.buckets).fill(null);
};
/**
* @param {number} key
* @return {number}
*/
MyHashSet.prototype.getBuckets = function (key) {
return key % this.buckets;
}
/**
* @param {number} key
* @return {number}
*/
MyHashSet.prototype.getBucketItems = function (key) {
return Math.floor(key / this.bucketItems);
}
/**
* @param {number} key
* @return {void}
*/
MyHashSet.prototype.add = function(key) {
const bucket = this.getBuckets(key);
const bucketItem = this.getBucketItems(key);
if (this.storage[bucket] === null) {
if ( bucket === 0 ) {
// to include upper bound which is 10 ^ 6 as it covers from 0 - 9999 and as this is an array
//if linked list is used we wouldn't need it (HashMap)
this.storage[bucket] = new Array(this.bucketItems + 1).fill(false)
}
this.storage[bucket] = new Array(this.bucketItems).fill(false)
}
this.storage[bucket][bucketItem] = true;
};
/**
* @param {number} key
* @return {void}
*/
MyHashSet.prototype.remove = function(key) {
const bucket = this.getBuckets(key);
const bucketItem = this.getBucketItems(key);
if (this.storage[bucket] === null) {
return;
}
this.storage[bucket][bucketItem] = false;
};
/**
* @param {number} key
* @return {boolean}
*/
MyHashSet.prototype.contains = function(key) {
const bucket = this.getBuckets(key);
const bucketItem = this.getBucketItems(key);
if (this.storage[bucket] === null) {
return false;
}
return this.storage[bucket][bucketItem]
};
/**
* Your MyHashSet object will be instantiated and called as such:
* var obj = new MyHashSet()
* obj.add(key)
* obj.remove(key)
* var param_3 = obj.contains(key)
*/