Skip to content

Commit 27da61b

Browse files
Merge pull request #47 from wakidurrahman/feat/MergeSortedArrays
added all file
2 parents 4fdce31 + 7e40458 commit 27da61b

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function mergeSortedArrays(firstArray, secondArray) {
2+
const mergedArray = []; // allocate empty array.
3+
let firstArrayItem = firstArray[0]; // get Array firstArray Index 0 value
4+
console.log(firstArrayItem);
5+
let secondArrayItem = secondArray[0]; // Get Array Index 0 value.
6+
console.log(secondArrayItem);
7+
let i = 1;
8+
let j = 1;
9+
10+
// We should actually move these 2 if statements to line 2 so that we do the checks before we do assignments in line 3 and 4!
11+
if (firstArray.length === 0) return secondArray;
12+
if (secondArray.length === 0) return firstArray;
13+
14+
while (firstArrayItem || secondArrayItem) {
15+
console.log(firstArrayItem, secondArrayItem);
16+
if (secondArrayItem === undefined || firstArrayItem < secondArrayItem) {
17+
mergedArray.push(firstArrayItem);
18+
firstArrayItem = firstArray[i];
19+
i++;
20+
} else {
21+
mergedArray.push(secondArrayItem);
22+
secondArrayItem = secondArray[j];
23+
j++;
24+
}
25+
}
26+
27+
return mergedArray;
28+
}
29+
// mergeSortedArrays([0, 3, 4, 31], [3, 4, 6, 30]);
30+
console.log(mergeSortedArrays([0, 3, 4, 31], [3, 4, 6, 30]));
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"use strict";
2+
exports.__esModule = true;
3+
exports.mergeSortedArrays = void 0;
4+
function mergeSortedArrays(firstArray, secondArray) {
5+
var mergedArray = []; // allocate empty array.
6+
var firstArrayItem = firstArray[0]; // get Array firstArray Index 0 value
7+
console.log(firstArrayItem);
8+
var secondArrayItem = secondArray[0]; // Get Array Index 0 value.
9+
console.log(secondArrayItem);
10+
var i = 1;
11+
var j = 1;
12+
// We should actually move these 2 if statements to line 2 so that we do the checks before we do assignments in line 3 and 4!
13+
if (firstArray.length === 0)
14+
return secondArray;
15+
if (secondArray.length === 0)
16+
return firstArray;
17+
while (firstArrayItem || secondArrayItem) {
18+
console.log(firstArrayItem, secondArrayItem);
19+
if (secondArrayItem === undefined || firstArrayItem < secondArrayItem) {
20+
mergedArray.push(firstArrayItem);
21+
firstArrayItem = firstArray[i];
22+
i++;
23+
}
24+
else {
25+
mergedArray.push(secondArrayItem);
26+
secondArrayItem = secondArray[j];
27+
j++;
28+
}
29+
}
30+
return mergedArray;
31+
}
32+
exports.mergeSortedArrays = mergeSortedArrays;
33+
// mergeSortedArrays([0, 3, 4, 31], [3, 4, 6, 30]);
34+
console.log(mergeSortedArrays([0, 3, 4, 31], [3, 4, 6, 30]));
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export function mergeSortedArrays(
2+
firstArray: number[],
3+
secondArray: number[]
4+
): number[] {
5+
const mergedArray: number[] = []; // allocate empty array.
6+
let firstArrayItem: number | undefined = firstArray[0]; // get Array firstArray Index 0 value
7+
console.log(firstArrayItem);
8+
let secondArrayItem: number | undefined = secondArray[0]; // Get Array Index 0 value.
9+
console.log(secondArrayItem);
10+
let i: number = 1;
11+
let j: number = 1;
12+
13+
// We should actually move these 2 if statements to line 2 so that we do the checks before we do assignments in line 3 and 4!
14+
if (firstArray.length === 0) return secondArray;
15+
if (secondArray.length === 0) return firstArray;
16+
17+
while (firstArrayItem || secondArrayItem) {
18+
console.log(firstArrayItem, secondArrayItem);
19+
if (secondArrayItem === undefined || firstArrayItem < secondArrayItem) {
20+
mergedArray.push(firstArrayItem);
21+
firstArrayItem = firstArray[i];
22+
i++;
23+
} else {
24+
mergedArray.push(secondArrayItem);
25+
secondArrayItem = secondArray[j];
26+
j++;
27+
}
28+
}
29+
30+
return mergedArray;
31+
}
32+
// mergeSortedArrays([0, 3, 4, 31], [3, 4, 6, 30]);
33+
console.log(mergeSortedArrays([0, 3, 4, 31], [3, 4, 6, 30]));

0 commit comments

Comments
 (0)