diff --git a/8_kyu/Merge two sorted arrays into one/README.md b/8_kyu/Merge two sorted arrays into one/README.md new file mode 100644 index 0000000..7815467 --- /dev/null +++ b/8_kyu/Merge two sorted arrays into one/README.md @@ -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] +``` diff --git a/8_kyu/Merge two sorted arrays into one/index.test.ts b/8_kyu/Merge two sorted arrays into one/index.test.ts new file mode 100644 index 0000000..723cc55 --- /dev/null +++ b/8_kyu/Merge two sorted arrays into one/index.test.ts @@ -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, + ]); + }); +}); diff --git a/8_kyu/Merge two sorted arrays into one/index.ts b/8_kyu/Merge two sorted arrays into one/index.ts new file mode 100644 index 0000000..8642771 --- /dev/null +++ b/8_kyu/Merge two sorted arrays into one/index.ts @@ -0,0 +1,3 @@ +export function mergeArrays(arr1: number[], arr2: number[]): number[] { + return [...new Set(arr1.concat(arr2))].sort((a, b) => a - b); +} diff --git a/README.md b/README.md index ce6b544..40ad172 100644 --- a/README.md +++ b/README.md @@ -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 \