Given 2 arrays [1,2] and [7,8] what is the most efficient way of merging that to form [[1,7], [2,8]]. I know we can do this:
a1 = [1,2], a2 = [7,8], a3=[];
for (var i=0; i<a1.length; i++) {
a3.push([a1[i], a2[i]]);
}
I am dealing with a large array. So I want to see if there is a better way.
There is no way to do this faster than O(n) because every element must be touched once.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With