Skip to content

Commit 5aa5050

Browse files
committed
feat: exercises from chapter 7
1 parent c222fcd commit 5aa5050

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

chapter-7/ex1.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function arrayIntersection(arr1: number[], arr2: number[]): number[] {
2+
let mappedArray = new Map<number, boolean>();
3+
for (const num of arr1) {
4+
mappedArray.set(num, true);
5+
}
6+
let intersection: number[] = [];
7+
8+
for (const num of arr2) {
9+
if (mappedArray.has(num)) {
10+
intersection.push(num);
11+
}
12+
}
13+
return intersection;
14+
}

chapter-7/ex2.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function findFirstDuplicate(array: string[]): string {
2+
const hash = new Map<string, boolean>()
3+
4+
for (const str of array) {
5+
if (hash.has(str)) {
6+
return str
7+
}
8+
hash.set(str, true)
9+
}
10+
return 'No duplicates found'
11+
}

0 commit comments

Comments
 (0)