-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhashTable.js
37 lines (35 loc) · 880 Bytes
/
hashTable.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
class HashTable{
constructor(){
this.hashTable = new Array(255);
this.tableSize = 0
}
set(key,val){
const index = this._hash(key);
this.hashTable[index] = [key,val];
this.tableSize++;
}
get(key){
const index = this._hash(key);
return this.hashTable[index];
}
remove(key){
const index = this._hash(key);
if(this.hashTable[index] && this.hashTable.length){
this.hashTable[index]=[];
return "removed";
}else{
return "key not avaliable"
}
}
_hash(key){
let hash = 0;
for(let i=0;i<key.length;i++){
hash+=key[i].charCodeAt();
}
return hash % this.hashTable.length;
}
}
const table = new HashTable()
table.set('vishal',56);
table.set('dinesh',4545);
console.log(table.get('vishal'))