diff --git a/snippets/mergeSortedArrays.md b/snippets/mergeSortedArrays.md new file mode 100644 index 000000000..fb23232c1 --- /dev/null +++ b/snippets/mergeSortedArrays.md @@ -0,0 +1,26 @@ +--- +title: mergeSortedArrays +tags: array,intermediate +--- + +Merges two sorted arrays into one. + +- Use the spread operator (`...`) to clone both of the given arrays. +- Use `Array.from()` to create an array of the appropriate length based on the given arrays. +- Use `Array.prototype.shift()` to populate the newly created array from the removed elements of the cloned arrays. + +```js +const mergeSortedArrays = (a, b) => { + const _a = [...a], + _b = [...b]; + return Array.from({ length: _a.length + _b.length }, () => { + if (!_a.length) return _b.shift(); + else if (!_b.length) return _a.shift(); + else return _a[0] > _b[0] ? _b.shift() : _a.shift(); + }); +}; +``` + +```js +mergeSortedArrays([1, 4, 5], [2, 3, 6]); // [1, 2, 3, 4, 5, 6] +```