Skip to content

Instantly share code, notes, and snippets.

@Abiola-Farounbi
Last active May 12, 2021 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Abiola-Farounbi/832f8d556f6041581f01cbdbe87082be to your computer and use it in GitHub Desktop.
Save Abiola-Farounbi/832f8d556f6041581f01cbdbe87082be to your computer and use it in GitHub Desktop.
The function returns the merged array of two sorted array
//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 ]
@meekg33k
Copy link

meekg33k commented May 12, 2021

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:

  • On line 9, you added a very good check to see if the input values were an array type and if they weren't you returned a 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.

  • Also on line 29, you wrote while( i < classA.length && j < classA.length), did you want to write while( i < classA.length && j < classB.length) instead? Where i is the counter for classA and j is the counter for classB?

Do let me know your thoughts.

@Abiola-Farounbi
Copy link
Author

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