Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update solution with sorting via object to lc problem: No.2722 #2847

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor: update solution with sorting via object to lc problem: No.2722
  • Loading branch information
rain84 committed May 19, 2024
commit 04fd6ae945c2c7fb44766fd8e2d9800fcb70d6fa
31 changes: 19 additions & 12 deletions solution/2700-2799/2722.Join Two Arrays by ID/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,25 @@ arr2 = [
#### TypeScript

```ts
function join(arr1: any[], arr2: any[]): any[] {
const d = new Map(arr1.map(x => [x.id, x]));
arr2.forEach(x => {
if (d.has(x.id)) {
d.set(x.id, { ...d.get(x.id), ...x });
} else {
d.set(x.id, x);
}
});
return [...d.values()].sort((a, b) => a.id - b.id);
}
```
function join(arr1: ArrayType[], arr2: ArrayType[]): ArrayType[] {
const r = (acc: Obj, x: ArrayType): Obj => ((acc[x.id] = x), acc)
const d = arr1.reduce(r, {})

arr2.forEach((x) => {
if (d[x.id]) {
Object.assign(d[x.id], x)
} else {
d[x.id] = x
}
})
return Object.values(d)
}

type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue }
type ArrayType = { id: number } & Record<string, JSONValue>

type Obj = Record<number, ArrayType>
```

<!-- tabs:end -->

Expand Down
31 changes: 19 additions & 12 deletions solution/2700-2799/2722.Join Two Arrays by ID/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,25 @@ arr2 = [
#### TypeScript

```ts
function join(arr1: any[], arr2: any[]): any[] {
const d = new Map(arr1.map(x => [x.id, x]));
arr2.forEach(x => {
if (d.has(x.id)) {
d.set(x.id, { ...d.get(x.id), ...x });
} else {
d.set(x.id, x);
}
});
return [...d.values()].sort((a, b) => a.id - b.id);
}
```
function join(arr1: ArrayType[], arr2: ArrayType[]): ArrayType[] {
const r = (acc: Obj, x: ArrayType): Obj => ((acc[x.id] = x), acc)
const d = arr1.reduce(r, {})

arr2.forEach((x) => {
if (d[x.id]) {
Object.assign(d[x.id], x)
} else {
d[x.id] = x
}
})
return Object.values(d)
}

type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue }
type ArrayType = { id: number } & Record<string, JSONValue>

type Obj = Record<number, ArrayType>
```

<!-- tabs:end -->

Expand Down
30 changes: 19 additions & 11 deletions solution/2700-2799/2722.Join Two Arrays by ID/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
function join(arr1: any[], arr2: any[]): any[] {
const d = new Map(arr1.map(x => [x.id, x]));
arr2.forEach(x => {
if (d.has(x.id)) {
d.set(x.id, { ...d.get(x.id), ...x });
} else {
d.set(x.id, x);
}
});
return [...d.values()].sort((a, b) => a.id - b.id);
}
function join(arr1: ArrayType[], arr2: ArrayType[]): ArrayType[] {
const r = (acc: Obj, x: ArrayType): Obj => ((acc[x.id] = x), acc)
const d = arr1.reduce(r, {})

arr2.forEach((x) => {
if (d[x.id]) {
Object.assign(d[x.id], x)
} else {
d[x.id] = x
}
})
return Object.values(d)
}

type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue }
type ArrayType = { id: number } & Record<string, JSONValue>

type Obj = Record<number, ArrayType>

Loading