-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.js
More file actions
61 lines (55 loc) · 1.23 KB
/
HashTable.js
File metadata and controls
61 lines (55 loc) · 1.23 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
/*
* @author Chiragkumar Maniyar
* @2019 all rights are reserved
*/
/**
* Custom hash table for understanding
*/
class HashTable {
constructor(size) {
this.data = new Array(size);
}
_hash(key) {
var hash = 0;
for (var i = 0; i < key.length; i++) {
hash = (hash + key.charCodeAt(i) * i) % this.data.length;
}
return hash;
}
set(key, value) {
var address = this._hash(key);
if (!this.data[address]) {
this.data[address] = [];
}
this.data[address].push([key, value]);
return this.data;
}
get(key) {
var address = this._hash(key);
var currentBucket = this.data[address];
if (currentBucket) {
for (var i = 0; i < currentBucket.length; i++) {
if (currentBucket[i][0] === key) {
return currentBucket[i][1];
}
}
}
return undefined;
}
keys() {
var keysArray = [];
for (var i = 0; i < this.data.length; i++) {
if (this.data[i]) {
keysArray.push(this.data[i][0][0]);
}
}
return keysArray;
}
}
// var table = new HashTable(10);
// table.set('grapes', 1000);
// table.set('mango', 23);
// table.set('apple', 10);
// table.set('banana', 12);
// table.set('avacado', 1);
// table.get('mango');