Skip to content

Commit d8cde61

Browse files
author
Wakidur Rahaman
committed
update solution
1 parent f4ba513 commit d8cde61

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ function firstRecurringCharacter(array: number[]): number | undefined {
2626
return undefined;
2727
}
2828

29+
// Solution 02:
30+
function firstRecurringCharacter2(array: number[]): undefined | number {
31+
let KeysMap: { [key: number]: number } = {};
32+
for (let i = 0; i < array.length; i++) {
33+
const element = array[i];
34+
if (KeysMap[array[i]] !== undefined) {
35+
return array[i];
36+
} else {
37+
KeysMap[array[i]] = i;
38+
}
39+
}
40+
return undefined;
41+
}
42+
2943
const array01 = [2, 5, 1, 2, 3, 5, 1, 2, 4];
3044
const array02 = [2, 1, 1, 2, 3, 5, 1, 2, 4];
3145
const array03 = [2, 3, 4, 5];

0 commit comments

Comments
 (0)