In this program merged two array and then sorted using temp.but this not correct method.because two array are sorted ,so method should be unique i.e. merging of two sorted i
The issue I see with most solutions here is that they don't precreate the array, just pushing into an array when you know the end game size is a little bit wasteful.
This is my suggestion, we can make it a little more efficient but it will make it less readable:
function mergeSortedArrays(arr1, arr2) {
let i1 = 0, i2 = 0;
return [...arr1, ...arr2].map(
() =>
i1 === arr1.length ? arr2[i2++] :
i2 === arr2.length ? arr1[i1++] :
arr1[i1] < arr2[i2]? arr1[i1++] :
arr2[i2++]
);
}
console.log(mergeSortedArrays([1,2,3,5,9],[4,6,7,8]))