-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashtable.js
More file actions
44 lines (39 loc) · 867 Bytes
/
Copy pathHashtable.js
File metadata and controls
44 lines (39 loc) · 867 Bytes
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
class HashTable {
constructor(size){
this.data = new Array(size);
}
_hash(key) {
let hash = 0;
for (let i =0; i < key.length; i++){
hash = (hash + key.charCodeAt(i) * i) % this.data.length
}
return hash;
}
set(key,value){
let address=this._hash(key)
if (!this.data[address]){
this.data[address]=[]
}
this.data[address].push([key,value])
return this.data
}
get(key){
let address=this._hash(key)
let container=this.data[address]
if (container){
for (let i=0;i<address.length;i++){
if(container[i][0]===key){
return container[i][1]
}
}
}
else{
return "undefined"
}
}
}
const myHashTable = new HashTable(2);
myHashTable.set('grapes', 10000)
myHashTable.set("shilu",21)
myHashTable.set("sachin",10)
console.log(myHashTable.get("grapes"))