Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6fc66ea

Browse files
Completed hash table implementation (#6 IP)
1 parent 7f68a70 commit 6fc66ea

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

‎Data-Structures/Arrays/My_Array_Implementation.ts renamed to ‎Data-Structures/Arrays/Implementation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: deno run Data-Structures/Arrays/My_Array_Implementation.ts
1+
// RUN: deno run Data-Structures/Arrays/Implementation.ts
22

33
type NumIndexedObject = { [index: number]: any };
44

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// RUN: deno run Data-Structures/Hash-Tables/Implementation.ts
2+
3+
class HashTable {
4+
private size: number;
5+
private data: Array<Array<any>>;
6+
7+
constructor(numBuckets: number) {
8+
this.size = numBuckets;
9+
this.data = new Array(numBuckets);
10+
}
11+
12+
public set(key: string, value: any): void {
13+
let address = this._hash(key);
14+
15+
if (!this.data[address]) {
16+
this.data[address] = [];
17+
}
18+
19+
this.data[address].push([key,value]);
20+
}
21+
22+
public get(key: string): any {
23+
let address = this._hash(key);
24+
const currentBucket: any[] = this.data[address];
25+
26+
if (currentBucket.length) {
27+
for(let item of currentBucket) {
28+
if (item[0] === key) return item[1];
29+
}
30+
}
31+
32+
return undefined;
33+
}
34+
35+
public keys(): string[] {
36+
const keysArray = new Array<string>();
37+
38+
// Loop through all buckets
39+
for (let i=0; i < this.data.length; ++i) {
40+
// If bucket has item(s)
41+
if (this.data[i]) {
42+
// Handle Collisions: Grab all keys from bucket
43+
// (incl. multiple items)
44+
for(let item of this.data[i]) {
45+
keysArray.push(item[0]);
46+
}
47+
}
48+
}
49+
50+
return keysArray;
51+
}
52+
53+
public values(): string[] {
54+
const valuesArray = new Array<string>();
55+
56+
for (let i=0; i < this.data.length; ++i) {
57+
if (this.data[i]) {
58+
for(let item of this.data[i]) {
59+
valuesArray.push(item[1]);
60+
}
61+
}
62+
}
63+
64+
return valuesArray;
65+
}
66+
67+
public getSize(): number {
68+
return this.size;
69+
}
70+
71+
private _hash(key: string) {
72+
let hash = 0;
73+
74+
for (let i=0; i < key.length; ++i) {
75+
hash = (hash + key.charCodeAt(i) * i)
76+
% this.data.length;
77+
}
78+
79+
return hash;
80+
}
81+
82+
public testHashFunction() {
83+
console.log(hashTable._hash('grapes'));
84+
console.log(hashTable._hash('grapess'));
85+
console.log(hashTable._hash('grapes'));
86+
}
87+
}
88+
89+
const hashTable = new HashTable(16);
90+
// hashTable.testHashFunction();
91+
92+
hashTable.set('grapes', 27);
93+
hashTable.set('apples', 6);
94+
hashTable.set('tangerines', 12);
95+
96+
console.log('apples:', hashTable.get('apples'));
97+
console.log('grapes:', hashTable.get('grapes'));
98+
99+
console.log(hashTable.keys());
100+
console.log(hashTable.values());
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//Google Question
2+
//Given an array = [2,5,1,2,3,5,1,2,4]:
3+
//It should return 2
4+
5+
//Given an array = [2,1,1,2,3,5,1,2,4]:
6+
//It should return 1
7+
8+
//Given an array = [2,3,4,5]:
9+
//It should return undefined
10+
11+
//Bonus... What if we had this:
12+
// [2,5,5,2,3,5,1,2,4]
13+
// return 5 because the pairs are before 2,2

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /