Skip to content

Instantly share code, notes, and snippets.

@deedee47
Last active May 13, 2021 14:52
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 deedee47/f474be71faba3157a61a3f2db67b4e43 to your computer and use it in GitHub Desktop.
Save deedee47/f474be71faba3157a61a3f2db67b4e43 to your computer and use it in GitHub Desktop.
Merge sorted arrays into one
public int[] mergeSortedArray(int[] classA, int[] classB){
if(classA == null && classB == null) return new int[0];
else if (classA == null) return classB;
else if (classB == null) return classA;
int aIndex = 0, bIndex = 0, mergeIndex = 0, classLength = classA.length+classB.length;
int[] merged = new int[classLength];
while(mergeIndex < classLength){
if(aIndex<classA.length && bIndex<classB.length){
merged[mergeIndex++] = (classA[aIndex] > classB[bIndex]) ? classB[bIndex++] : classA[aIndex++];
}
else if(aIndex >= classA.length){
merged[mergeIndex++] = classB[bIndex++];
}
else if(bIndex >= classB.length){
merged[mergeIndex++] = classA[aIndex++];
}
}
return merged;
}
@meekg33k
Copy link

meekg33k commented May 13, 2021

Hello @deedee47, 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 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