Skip to content

Instantly share code, notes, and snippets.

@tobi-bams
Created May 9, 2021 14:59
Show Gist options
  • Save tobi-bams/f41fcf3f4d98f6ebf1ecbb7ad9dfa283 to your computer and use it in GitHub Desktop.
Save tobi-bams/f41fcf3f4d98f6ebf1ecbb7ad9dfa283 to your computer and use it in GitHub Desktop.
const sortFunction = (firstArray, secondArray) => {
if (
(!Array.isArray(firstArray) || firstArray.length === 0) &&
(!Array.isArray(secondArray) || secondArray.length === 0)
) {
return [];
}
if (!Array.isArray(firstArray) || firstArray.length === 0) {
return secondArray;
}
if (!Array.isArray(secondArray) || secondArray.length === 0) {
return firstArray;
}
let finalSortedArray = [];
let firstArrayIndex = 0;
let secondArrayIndex = 0;
let finalSortedArrayIndex = 0;
while (finalSortedArrayIndex < firstArray.length + secondArray.length) {
let firstArrayEnd = firstArrayIndex >= firstArray.length;
let secondArrayEnd = secondArrayIndex >= secondArray.length;
if (
!firstArrayEnd &&
(secondArrayEnd ||
firstArray[firstArrayIndex] < secondArray[secondArrayIndex])
) {
finalSortedArray[finalSortedArrayIndex] = firstArray[firstArrayIndex];
firstArrayIndex += 1;
} else {
finalSortedArray[finalSortedArrayIndex] = secondArray[secondArrayIndex];
secondArrayIndex += 1;
}
finalSortedArrayIndex += 1;
}
return finalSortedArray;
};
@meekg33k
Copy link

meekg33k commented May 13, 2021

Hello @tobi-bams, thank you for participating in Week 5 of #AlgorithmFridays.

Congratulations! 🎉🎉, your solution has been selected as one of the 12 winning solutions for Week 5 of #AlgorithmFridays.

Your solution was selected because it is clean, robust, had the best time-efficiency and passes all of the test cases. Kudos to you!

However since only 3 solutions can be awarded the $20 prize, there will be a 'raffle draw' tomorrow Friday May 14 at 9.00 am WAT (1.00 am PST) to select 3 out of the 12 solutions in a fair manner.

If you will to participate in the raffle draw, please send an email to uduak@meekg33k.dev or send me a DM on Twitter @meekg33k so I can share the meeting link with you.

Congratulations once again and thank you for participating. See you tomorrow for Week 6 of #AlgorithmFridays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment