You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.
Merge nums1 and nums2 into a single array sorted in non-decreasing order.
Example 1
Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
Explanation: The arrays we are merging are [1,2,3] and [2,5,6]. The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1.
function merge(nums1, m, nums2, n) {
let looped = nums1.map((e) => {
return e;
})
let numbers = looped.splice(nums1.length - n, m);
let merged = numbers.concat(nums2);
return merged.sort(((a, b) => {
return a - b;
}));
};
merge([1,2,3,0,0,0], 3 , [2,5,6],3)
Expect Output:
[1,2,2,3,5,6]
[1]
[1]
My Output:
[1,2,3,0,0,0]
[1]
[0]