-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day73.js
37 lines (32 loc) · 951 Bytes
/
Day73.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//* Merge two sorted arrays
function mergeSortedArrays(arr1, arr2) {
let mergedArray = [];
let i = 0;
let j = 0;
// Compare elements from both arrays and push the smaller one into the mergedArray
while (i < arr1.length && j < arr2.length) {
if (arr1[i] < arr2[j]) {
mergedArray.push(arr1[i]);
i++;
} else {
mergedArray.push(arr2[j]);
j++;
}
}
// If there are remaining elements in arr1, add them to mergedArray
while (i < arr1.length) {
mergedArray.push(arr1[i]);
i++;
}
// If there are remaining elements in arr2, add them to mergedArray
while (j < arr2.length) {
mergedArray.push(arr2[j]);
j++;
}
return mergedArray;
}
// Example usage:
let array1 = [1, 3, 5, 7];
let array2 = [2, 4, 6, 8];
let result = mergeSortedArrays(array1, array2);
console.log(result); // [1, 2, 3, 4, 5, 6, 7, 8]