-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproblem-11.js
More file actions
34 lines (27 loc) · 728 Bytes
/
problem-11.js
File metadata and controls
34 lines (27 loc) · 728 Bytes
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
// Merge Two Sorted Arrays
// Write a function that merges two sorted arrays into one sorted array.
// Example: mergeSortedArrays([1, 3, 5], [2, 4, 6]) → [1, 2, 3, 4, 5, 6]
function mergeSortedArrays(arr1, arr2) {
let i = 0, j = 0;
const result = [];
while (i < arr1.length && j < arr2.length) {
if (arr1[i] < arr2[j]) {
result.push(arr1[i]);
i++;
}
else {
result.push(arr2[j])
j++;
}
}
while (j < arr2.length) {
result.push(arr2[j]);
j++;
}
while (i < arr1.length) {
result.push(arr1[i]);
i++;
}
console.log(result)
}
mergeSortedArrays([1, 3, 5, 8, 9, 10], [2, 4, 6, 7])