merge sorted array problem leetcode problem please help me with this code I have mentioned my piece of code and image of my input vs required output

var merge = function(nums1, m, nums2, n) {

let first = m-1;

for(let i = n-1; i>=0;i-- )
    {
        if(nums1[first]>nums2[i])
            {
                  nums1.splice[first,0,nums2[i]]
            }
        else{ 
                nums1.splice(first+1,0,nums2[i])
        }
    }
nums1.splice(m+n-1)

}; [enter image description here][1]

[1]: https://i.stack.imgur.com/r2jAC.png**strong text**

1 answer

  • answered 2021-07-27 19:11 Nikola Pavicevic

    You can use slice and concat

    const arr1= [1,2,3,0,0]
    const arr2 = [2,5,6]
    const n1 = 3
    const n2 = 3
    
    const merge = (arr1, n1, arr2, n2) => {
      return arr1.splice(0, n1).concat(arr2.splice(0, n2)).sort()
    }
    
    console.log(merge(arr1, n1, arr2, n2))

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum