-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashMap.js
79 lines (64 loc) · 1.88 KB
/
hashMap.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
class HashTable {
constructor(size = 17) {
this.keyMap = new Array(size);
}
_hash(key) {
let total = 0;
const SOMEPRIME = 53;
for (let i = 0; i < Math.min(key.length, 100); i++) {
let charCode = key[i].charCodeAt(0);
total = (total * SOMEPRIME + charCode) % this.keyMap.length;
}
return total;
}
set(key, value) {
const index = this._hash(key);
// If the index is empty, create a new nested array at the index
!this.keyMap[index] && (this.keyMap[index] = []);
this.keyMap[index].push([key, value]);
}
get(key) {
const index = this._hash(key);
if (!this.keyMap[index]) return undefined;
// Loop over the sub array
for (let i = 0; i < this.keyMap[index].length; i++) {
// keyMap[index[i[0=>key, 1=>val], i[otherKey, otherVal])]]
if (this.keyMap[index][i][0] === key)
return this.keyMap[index][i][1];
}
}
keys() {
let keysArr = [];
for (let i = 0; i < this.keyMap.length; i++) {
if (!this.keyMap[i]) continue;
// Loop over sub array
for (let j = 0; j < this.keyMap[i].length; j++) {
if (keysArr.includes(this.keyMap[i][j][0])) continue;
keysArr.push(this.keyMap[i][j][0]);
}
}
return keysArr;
}
values() {
let valsArr = [];
for (let i = 0; i < this.keyMap.length; i++) {
if (!this.keyMap[i]) continue;
// Loop over sub array
for (let j = 0; j < this.keyMap[i].length; j++) {
if (valsArr.includes(this.keyMap[i][j][1])) continue;
valsArr.push(this.keyMap[i][j][1]);
}
}
console.log(valsArr);
return valsArr;
}
}
const hashMap = new HashTable();
hashMap.set('apple', '🍏');
hashMap.set('mango', '🥭');
hashMap.set('banana', '🍌');
hashMap.set('orange', '🍊');
hashMap.set('melon', '🍉');
hashMap.keys();
// console.log(hashMap.get('apple'));
// console.log(hashMap.keyMap);