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
Changes from all commits
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
19 changes: 13 additions & 6 deletions solution/2700-2799/2722.Join Two Arrays by ID/README.md
Original file line number Diff line number Diff line change
@@ -109,17 +109,24 @@ arr2 = [
#### TypeScript

```ts
function join(arr1: any[], arr2: any[]): any[] {
const d = new Map(arr1.map(x => [x.id, x]));
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.has(x.id)) {
d.set(x.id, { ...d.get(x.id), ...x });
if (d[x.id]) {
Object.assign(d[x.id], x);
} else {
d.set(x.id, x);
d[x.id] = x;
}
});
return [...d.values()].sort((a, b) => a.id - b.id);
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 -->
19 changes: 13 additions & 6 deletions solution/2700-2799/2722.Join Two Arrays by ID/README_EN.md
Original file line number Diff line number Diff line change
@@ -107,17 +107,24 @@ arr2 = [
#### TypeScript

```ts
function join(arr1: any[], arr2: any[]): any[] {
const d = new Map(arr1.map(x => [x.id, x]));
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.has(x.id)) {
d.set(x.id, { ...d.get(x.id), ...x });
if (d[x.id]) {
Object.assign(d[x.id], x);
} else {
d.set(x.id, x);
d[x.id] = x;
}
});
return [...d.values()].sort((a, b) => a.id - b.id);
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 -->
19 changes: 13 additions & 6 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,18 @@
function join(arr1: any[], arr2: any[]): any[] {
const d = new Map(arr1.map(x => [x.id, x]));
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.has(x.id)) {
d.set(x.id, { ...d.get(x.id), ...x });
if (d[x.id]) {
Object.assign(d[x.id], x);
} else {
d.set(x.id, x);
d[x.id] = x;
}
});
return [...d.values()].sort((a, b) => a.id - b.id);
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>;