-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash_Tables.js
More file actions
79 lines (71 loc) · 1.91 KB
/
Hash_Tables.js
File metadata and controls
79 lines (71 loc) · 1.91 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class HashTable {
constructor(size) {
this.data = new Array(size);
// this.data = [];
}
_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) {
const address = this._hash(key);
const currentBucket = this.data[address];
if (currentBucket) {
for (let i = 0; i < currentBucket.length; i++) {
if (currentBucket[i][0] === key) {
return currentBucket[i][1];
}
}
}
return undefined;
}
keys() {
const keysArray = [];
console.log(this.data.length);
for (let i = 0; i < this.data.length; i++) {
if (this.data[i]) {
keysArray.push(this.data[i][0][0]);
}
}
return keysArray;
}
}
const myHashTable = new HashTable(50);
// myHashTable.set('grapes', 10000)
// myHashTable.set('grapes', 10000)
// myHashTable.get('grapes')
// myHashTable.set('apples', 9)
// myHashTable.get('apples')
// myHashTable.keys()
// const findRecuringVal = (arr) => {
// for(let i = 0; i < arr.length; i++) {
// for(let j = i + 1; j < arr.length; j++) {
// if(arr[i] === arr[j]) {
// return arr[i];
// }
// }
// }
// };
const findRecuringValHash = (arr) => {
let map = {};
for (let i = 0; i < arr.length; i++) {
if (map[arr[i]] !== undefined) {
return arr[i];
} else {
map[arr[i]] = i;
}
console.log("map :::", map);
}
};
console.log(findRecuringValHash([2, 4, 4, 2, 3, 4, 2, 2]));