Skip to content

Commit 82754ae

Browse files
author
Wakidur Rahaman
committed
updated both
1 parent 9f4efdc commit 82754ae

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

src/course-master-the-coding/data-structures/hash-tables/hash-tables-03.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ var HashTableMain = /** @class */ (function () {
3939
/**
4040
* keys
4141
*/
42-
HashTableMain.prototype.keys = function () {
42+
HashTableMain.prototype.keysOld = function () {
4343
var keysArray = [];
4444
console.log(this.data.length);
4545
for (var i = 0; i < this.data.length; i++) {
@@ -52,6 +52,28 @@ var HashTableMain = /** @class */ (function () {
5252
}
5353
return keysArray;
5454
};
55+
HashTableMain.prototype.keys = function () {
56+
if (!this.data.length) {
57+
return undefined;
58+
}
59+
var result = [];
60+
// loop through all the elements
61+
for (var i = 0; i < this.data.length; i++) {
62+
// if it's not an empty memory cell
63+
if (this.data[i] && this.data[i].length) {
64+
// but also loop through all the potential collisions
65+
if (this.data.length > 1) {
66+
for (var j = 0; j < this.data[i].length; j++) {
67+
result.push(this.data[i][j][0]);
68+
}
69+
}
70+
else {
71+
result.push(this.data[i][0]);
72+
}
73+
}
74+
}
75+
return result;
76+
};
5577
return HashTableMain;
5678
}());
5779
// Example
@@ -64,3 +86,24 @@ myHashTable01.set('banana', 'Nice foots');
6486
myHashTable01.get('banana');
6587
myHashTable01.keys();
6688
console.log(myHashTable01.data);
89+
console.log(myHashTable01.keys());
90+
/**
91+
*
92+
*
93+
*
94+
[
95+
[
96+
[ 'a', 'Nice foots' ],
97+
[ 'b', 'Nice foots' ],
98+
[ 'c', 'Nice foots' ],
99+
[ 'd', 'Nice foots' ],
100+
[ 'e', 'Nice foots' ],
101+
[ 'f', 'Nice foots' ],
102+
[ 'g', 'Nice foots' ]
103+
],
104+
<2 empty items>,
105+
[ [ 'grapes', 10000 ], [ 'banana', 'Nice foots' ] ],
106+
<5 empty items>,
107+
[ [ 'apples', 9 ] ]
108+
]
109+
*/

src/course-master-the-coding/data-structures/hash-tables/hash-tables-03.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class HashTableMain {
4444
/**
4545
* keys
4646
*/
47-
public keys(): (number | string)[] {
47+
public keysOld(): (number | string)[] {
4848
const keysArray: (number | string)[] = [];
4949
console.log(this.data.length);
5050
for (let i = 0; i < this.data.length; i++) {
@@ -57,6 +57,28 @@ class HashTableMain {
5757
}
5858
return keysArray;
5959
}
60+
61+
public keys(): undefined | (string | [string, string | number])[] {
62+
if (!this.data.length) {
63+
return undefined;
64+
}
65+
let result: (string | [string, string | number])[] = [];
66+
// loop through all the elements
67+
for (let i = 0; i < this.data.length; i++) {
68+
// if it's not an empty memory cell
69+
if (this.data[i] && this.data[i].length) {
70+
// but also loop through all the potential collisions
71+
if (this.data.length > 1) {
72+
for (let j = 0; j < this.data[i].length; j++) {
73+
result.push(this.data[i][j][0]);
74+
}
75+
} else {
76+
result.push(this.data[i][0]);
77+
}
78+
}
79+
}
80+
return result;
81+
}
6082
}
6183

6284
// Example
@@ -69,3 +91,25 @@ myHashTable01.set('banana', 'Nice foots');
6991
myHashTable01.get('banana');
7092
myHashTable01.keys();
7193
console.log(myHashTable01.data);
94+
console.log(myHashTable01.keys());
95+
96+
/**
97+
*
98+
*
99+
*
100+
[
101+
[
102+
[ 'a', 'Nice foots' ],
103+
[ 'b', 'Nice foots' ],
104+
[ 'c', 'Nice foots' ],
105+
[ 'd', 'Nice foots' ],
106+
[ 'e', 'Nice foots' ],
107+
[ 'f', 'Nice foots' ],
108+
[ 'g', 'Nice foots' ]
109+
],
110+
<2 empty items>,
111+
[ [ 'grapes', 10000 ], [ 'banana', 'Nice foots' ] ],
112+
<5 empty items>,
113+
[ [ 'apples', 9 ] ]
114+
]
115+
*/

0 commit comments

Comments
 (0)