Last active
May 12, 2021 13:22
-
-
Save Abiola-Farounbi/832f8d556f6041581f01cbdbe87082be to your computer and use it in GitHub Desktop.
The function returns the merged array of two sorted array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Problem Set for #AlgorithmFriday Week5 | |
// A school has two classes A and B, Each class has the ages of its students in a sorted list. | |
// The school decides to join both classes and needs you to help them write a function that merges both lists into one | |
// such that the students' ages still remains sorted in increasing order. | |
const mergeSortedArray = (classA,classB) => { | |
// Test cases for null and empty array | |
if (!Array.isArray(classA) || !Array.isArray(classB) ) return [] | |
if(classA.length == 0 && classB.length == 0 ) { | |
return []; | |
} | |
else if(classA.length == 0 ){ | |
return classB | |
} | |
else if(classB.length == 0 ){ | |
return classA | |
} | |
// intialize merged array | |
let finalArr = new Array(classA.length + classB.length) | |
let i = 0 | |
let j = 0 | |
let k = 0 | |
// sorting , then merging to final array | |
while( i < classA.length && j < classB.length){ | |
if(classA[i] < classB[j]){ | |
finalArr[k] = classA[i] | |
k = k + 1 | |
i = i + 1 | |
} | |
else{ | |
finalArr[k] = classB[j] | |
k = k + 1 | |
j = j + 1 | |
} | |
} | |
// adding the remaining elements in the array | |
while( i < classA.length ){ | |
finalArr[k] = classA[i] | |
k = k + 1 | |
i = i + 1 | |
} | |
while (j < classB.length ){ | |
finalArr[k] = classB[j] | |
k = k + 1 | |
j = j + 1 | |
} | |
return finalArr | |
} | |
console.log(mergeSortedArray([13,15,19],[11,13,18])) //return [ 11, 13, 13, 15, 18, 19 ] |
Thanks for the feedback.
As regards the validations, I was a bit confused on what to use but thanks for pointing it out.
Then for line 29, that was a typo error; It was meant to be while( i < classA.length && j < classB.length)
, which I have fixed it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @Abiola-Farounbi, thank you for participating in Week 5 of #AlgorithmFridays.
This is a decent attempt at solving the problem and I like your approach of concatenating both arrays and then populating the values of the array.
So a couple of thoughts:
string
. That was an unchecked assumption you made because this function expects the return type to always be an array. One approach would have been to throw an error or even just return an empty list. The best thing would have been to confirm what would be the preferred way to handle this edge case.I talked about the importance of validating your assumptions when solving technical problems in this article here, you might find it useful.
while( i < classA.length && j < classA.length)
, did you want to writewhile( i < classA.length && j < classB.length)
instead? Wherei
is the counter forclassA
andj
is thecounter
forclassB
?Do let me know your thoughts.