What is an elegant algorithm to mix the elements in two arrays (of potentially differing sizes) so that the items are drawn in an alternating fashion from each array, with the leftovers added to the end?
E.g.
Array 1 – A, B, C, D, E, F, G
Array 2 – 1, 2, 3, 4
Mixed array – A, 1, B, 2, C, 3, D, 4, E, F, G
I would prefer the solution in C#, but I should be able to read and transpose solutions in any language (or even some form of pseudo code).
Don’t worry about null checking or any other edge cases, I’ll handle those.
You mean something along the lines of this?
If you use this, you’ll probably want to store the array lengths in variables so you don’t have to keep calling a size() method (or whatever it is in whatever language you use).