-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHashSet.js
More file actions
68 lines (62 loc) · 1.65 KB
/
Copy pathHashSet.js
File metadata and controls
68 lines (62 loc) · 1.65 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
class HashSet {
constructor(size = 53) {
this.keyMap = new Array(size);
}
_hash(key) {
let total = 0;
const PRIME = 31;
for (let i = 0; i < Math.min(key.length, 100); i++) {
const char = key[i];
const value = char.charCodeAt(0) - 96;
total = (total * PRIME + value) % this.keyMap.length;
}
return total;
}
add(value) {
const index = this._hash(value);
if (!this.keyMap[index]) {
this.keyMap[index] = [];
}
if (!this.keyMap[index].includes(value)) {
this.keyMap[index].push(value);
}
}
remove(value) {
const index = this._hash(value);
if (this.keyMap[index]) {
const valueIndex = this.keyMap[index].indexOf(value);
if (valueIndex !== -1) {
this.keyMap[index].splice(valueIndex, 1);
return true;
}
}
return false;
}
contains(value) {
const index = this._hash(value);
if (this.keyMap[index]) {
return this.keyMap[index].includes(value);
}
return false;
}
values() {
let valuesArray = [];
for (let i = 0; i < this.keyMap.length; i++) {
if (this.keyMap[i]) {
valuesArray = valuesArray.concat(this.keyMap[i]);
}
}
return valuesArray;
}
}
// Example usage:
const hashSet = new HashSet();
hashSet.add("apple");
hashSet.add("banana");
hashSet.add("cherry");
console.log(hashSet.contains("apple")); // Output: true
console.log(hashSet.contains("grape")); // Output: false
hashSet.add("apple"); // Trying to add duplicate value
console.log(hashSet.values()); // Output: ["apple", "banana", "cherry"]
hashSet.remove("banana");
console.log(hashSet.values()); // Output: ["apple", "cherry"]