Skip to content

Commit 1b6766f

Browse files
author
Wakidur Rahaman
committed
added
1 parent d8cde61 commit 1b6766f

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function firstRecurringCharacter(array) {
1414
console.log('first : ', element);
1515
for (var j = i + 1; j < array.length; j++) {
1616
var element_1 = array[j];
17-
console.log('Second : ', element, element_1);
17+
console.log('Second : ', element_1);
1818
if (array[i] === array[j]) {
1919
console.log(array[i]);
2020
return array[i];
@@ -24,8 +24,28 @@ function firstRecurringCharacter(array) {
2424
console.log('Nothing match : undefined');
2525
return undefined;
2626
}
27-
var array01 = [2, 5, 1, 0, 3, 5, 1, 2, 4];
27+
// Solution 02:
28+
function firstRecurringCharacter2(array) {
29+
var KeysMap = {};
30+
for (var i = 0; i < array.length; i++) {
31+
var element = array[i];
32+
console.log(element);
33+
console.log(KeysMap);
34+
if (KeysMap[array[i]] !== undefined) {
35+
// if (KeysMap[array[i]] !== i) {
36+
console.log(KeysMap[array[i]], i);
37+
console.log(array[i]);
38+
return array[i];
39+
} else {
40+
KeysMap[array[i]] = i;
41+
}
42+
}
43+
44+
return undefined;
45+
}
46+
var array01 = [2, 5, 1, 2, 3, 5, 1, 2, 4];
2847
var array02 = [2, 1, 1, 2, 3, 5, 1, 2, 4];
2948
var array03 = [2, 3, 4, 5];
3049
// Call function
31-
firstRecurringCharacter(array01);
50+
// firstRecurringCharacter(array01);
51+
firstRecurringCharacter2(array02);

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ function firstRecurringCharacter2(array: number[]): undefined | number {
3131
let KeysMap: { [key: number]: number } = {};
3232
for (let i = 0; i < array.length; i++) {
3333
const element = array[i];
34-
if (KeysMap[array[i]] !== undefined) {
34+
console.log(element);
35+
console.log(KeysMap);
36+
// if (KeysMap[array[i]] !== undefined) {
37+
if (KeysMap[array[i]] === i) {
38+
console.log(KeysMap[array[i]], i);
3539
return array[i];
3640
} else {
3741
KeysMap[array[i]] = i;
@@ -44,4 +48,5 @@ const array01 = [2, 5, 1, 2, 3, 5, 1, 2, 4];
4448
const array02 = [2, 1, 1, 2, 3, 5, 1, 2, 4];
4549
const array03 = [2, 3, 4, 5];
4650
// Call function
47-
firstRecurringCharacter(array01);
51+
// firstRecurringCharacter(array01);
52+
firstRecurringCharacter2(array02);

0 commit comments

Comments
 (0)