-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.js
More file actions
48 lines (46 loc) · 1.26 KB
/
Copy pathHashTable.js
File metadata and controls
48 lines (46 loc) · 1.26 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
class HT {
constructor(size = 7) {
this.dataMap = new Array(size)
}
_hash(key) {
let intialHashVal = 0
let hash;
for (let i = 0; i < key.length; i++) {
hash = (intialHashVal + key.charCodeAt(i) * 23) % this.dataMap.length;
}
return hash;
}
set(key, value) {
let index = this._hash(key)
if(!this.dataMap[index]) {
this.dataMap[index] = []
}
this.dataMap[index].push([key, value])
}
get(key) {
let index = this._hash(key)
if(this.dataMap[index]) {
for(let i = 0; i < this.dataMap[index].length; i++) {
if(this.dataMap[index][i][0] == key) {
return this.dataMap[index][i][1]
}
}
}
}
keys() {
const allKeys = []
for (let i = 0; i < this.dataMap.length; i++) {
if(this.dataMap[i]) {
for (let j = 0; j < this.dataMap[i].length; j++) {
allKeys.push(this.dataMap[i][j][0])
}
}
}
return allKeys;
}
}
const ht = new HT()
ht.set('firstName', 'pankaj')
ht.set('lastName', 'pankaj')
ht.set('email', 'pankaj.me2k9@gmail.com')
console.log(ht.keys())