Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most efficient way of merging [1,2] and [7,8] into [[1,7], [2,8]]

Tags:

javascript

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.

like image 539
Subbu Avatar asked Dec 16 '22 01:12

Subbu


1 Answers

There is no way to do this faster than O(n) because every element must be touched once.

like image 123
Travis J Avatar answered Feb 15 '23 10:02

Travis J