Skip to content

Commit fcd2856

Browse files
author
Wakidur Rahaman
committed
update hash
1 parent 792e65d commit fcd2856

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function firstRecurringCharacter(array: number[]): number | undefined {
2424
}
2525
console.log('Nothing match : undefined');
2626
return undefined;
27-
}
27+
} // O(n^2)
2828

2929
// Solution 02:
3030
function firstRecurringCharacter2(array: number[]): undefined | number {
@@ -33,16 +33,26 @@ function firstRecurringCharacter2(array: number[]): undefined | number {
3333
const element = array[i];
3434
console.log(element);
3535
console.log(KeysMap);
36-
// if (KeysMap[array[i]] !== undefined) {
37-
if (KeysMap[array[i]] === i) {
36+
if (KeysMap[array[i]] !== undefined) {
3837
console.log(KeysMap[array[i]], i);
3938
return array[i];
4039
} else {
4140
KeysMap[array[i]] = i;
4241
}
4342
}
4443
return undefined;
45-
}
44+
} // O(n)
45+
46+
// Solution 02:
47+
const firstRecurringCharacter3 = (array: number[]): undefined | number => {
48+
const set: Set<number> = new Set();
49+
for (let i = 0; i < array.length; i++) {
50+
if (set.has(array[i])) return array[i];
51+
52+
set.add(array[i]);
53+
}
54+
return undefined;
55+
}; // O(n)
4656

4757
const array01 = [2, 5, 1, 2, 3, 5, 1, 2, 4];
4858
const array02 = [2, 1, 1, 2, 3, 5, 1, 2, 4];

0 commit comments

Comments
 (0)