Skip to content

Commit 950c5b9

Browse files
committed
feat: add ts solution to lc problem: No.2722
No.2722.Join Two Arrays by ID
1 parent 36fc915 commit 950c5b9

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

solution/2700-2799/2722.Join Two Arrays by ID/README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,17 @@ arr2 = [
9595
<!-- 这里可写当前语言的特殊实现逻辑 -->
9696

9797
```ts
98-
98+
function join(arr1: any[], arr2: any[]): any[] {
99+
const d = new Map(arr1.map(x => [x.id, x]));
100+
arr2.forEach(x => {
101+
if (d.has(x.id)) {
102+
d.set(x.id, { ...d.get(x.id), ...x });
103+
} else {
104+
d.set(x.id, x);
105+
}
106+
});
107+
return [...d.values()].sort((a, b) => a.id - b.id);
108+
}
99109
```
100110

101111
<!-- tabs:end -->

solution/2700-2799/2722.Join Two Arrays by ID/README_EN.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,17 @@ arr2 = [
8989
### **TypeScript**
9090

9191
```ts
92-
92+
function join(arr1: any[], arr2: any[]): any[] {
93+
const d = new Map(arr1.map(x => [x.id, x]));
94+
arr2.forEach(x => {
95+
if (d.has(x.id)) {
96+
d.set(x.id, { ...d.get(x.id), ...x });
97+
} else {
98+
d.set(x.id, x);
99+
}
100+
});
101+
return [...d.values()].sort((a, b) => a.id - b.id);
102+
}
93103
```
94104

95105
<!-- tabs:end -->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function join(arr1: any[], arr2: any[]): any[] {
2+
const d = new Map(arr1.map(x => [x.id, x]));
3+
arr2.forEach(x => {
4+
if (d.has(x.id)) {
5+
d.set(x.id, { ...d.get(x.id), ...x });
6+
} else {
7+
d.set(x.id, x);
8+
}
9+
});
10+
return [...d.values()].sort((a, b) => a.id - b.id);
11+
}

0 commit comments

Comments
 (0)