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
function merge(arr1, arr2) {
const arr = [];
while (arr1.length && arr2.length) {
if (arr1[0] < arr2[0]) {
arr.push(arr1.shift());
} else {
arr.push(arr2.shift());
}
}
return [...arr, ...arr1, ...arr2];
}
* If you want to merge the arrays in-place, clone each array and use it in the while loop instead.
Example: clonedArr1 = [...arr1];