Skip to content

Commit

Permalink
Merge two sorted arrays into one
Browse files Browse the repository at this point in the history
  • Loading branch information
PheRum committed May 19, 2024
1 parent 3d7f4c8 commit a275965
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
17 changes: 17 additions & 0 deletions 8_kyu/Merge two sorted arrays into one/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Merge two sorted arrays into one

You are given two sorted arrays that both only contain integers. Your task is to find a way to merge them into a single one, sorted in asc order. Complete the function mergeArrays(arr1, arr2), where arr1 and arr2 are the original sorted arrays.

You don't need to worry about validation, since arr1 and arr2 must be arrays with 0 or more Integers. If both arr1 and arr2 are empty, then just return an empty array.

Note: arr1 and arr2 may be sorted in different orders. Also arr1 and arr2 may have same integers. Remove duplicated in the returned result.

**Examples (input -> output)**

```
* [1, 2, 3, 4, 5], [6, 7, 8, 9, 10] -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
* [1, 3, 5, 7, 9], [10, 8, 6, 4, 2] -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
* [1, 3, 5, 7, 9, 11, 12], [1, 2, 3, 4, 5, 10, 12] -> [1, 2, 3, 4, 5, 7, 9, 10, 11, 12]
```
11 changes: 11 additions & 0 deletions 8_kyu/Merge two sorted arrays into one/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { mergeArrays } from "./index";

describe("Tests", () => {
it("test", () => {
expect(mergeArrays([1, 2, 3, 4], [5, 6, 7, 8])).toEqual([1, 2, 3, 4, 5, 6, 7, 8]);
expect(mergeArrays([1, 3, 5, 7, 9], [10, 8, 6, 4, 2])).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
expect(mergeArrays([1, 3, 5, 7, 9, 11, 12], [1, 2, 3, 4, 5, 10, 12])).toEqual([
1, 2, 3, 4, 5, 7, 9, 10, 11, 12,
]);
});
});
3 changes: 3 additions & 0 deletions 8_kyu/Merge two sorted arrays into one/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function mergeArrays(arr1: number[], arr2: number[]): number[] {
return [...new Set(arr1.concat(arr2))].sort((a, b) => a - b);
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

### Katas solved

`Total`: 43 \
`8_kyu`: 41 \
`Total`: 44 \
`8_kyu`: 42 \
`7_kyu`: 0 \
`6_kyu`: 2 \
`5_kyu`: 0 \
Expand Down

0 comments on commit a275965

Please sign in to comment.