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

2024/2/27 8:39:52

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.

Answer

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

http://en.ppmy.cn/q/42081.html

Related Q&A

How to set the focus to a child window without refreshing it?

I am having a web page which contain four links, each link open a popup window. I am using a common JavaScript function to open popup window, as given belowfunction OpenPopup(pageURL, title,w,h) {var l…

Need help in correcting issues in CRC-ITU check method written in Javascript (node.js)

We are trying to code GPS device listener on Javascript.While doing this, we are unable to develop the right script for the CRC-ITU error check. The explanation for the crc code generation from protoco…

split a mathematical expression on operators and include the operators in the output array

Im trying to split the mathematical strings on maths operators. for exampleexpression = "7*6+3/2-5*6+(7-2)*5"I need to tokenize it to produce:expressionArray = ["7","*",&q…

Correct audio volume output decibels using HTML5 / javascript, turn off pre-processing

We have observed that using javascript-based audio from a browser, the output level is not correctly linearly proportional to the input signal level on many systems. For example, if you play two sin w…

WebRTC - getting malformed constraints object from Chrome but not Firefox

I wonder what Im doing wrong.Im getting malformed constraints object errors from this:pc.createAnswer( function (answer) {... }, fail, { offerToReceiveAudio: true, offerToReceiveVideo: true });Any idea…

display a div below a selected input field? No JQuery

how to display a div everytime a user focus on a input field. there is already a div and it is hidden. the position of the div will change depending on the position of the selected field and it will be…

Shared Collection Between Client and Server Meteor

Beginner at Meteor. Just learning how everything works so bear with me.Everything was working fine in one file, but after installing iron:router to have a multi-page application, I realized that its be…

Div contenteditable : get and delete word preceding caret

Thanks to this question and answer posted by Tim Down, I made a function to get the word preceding caret in a "contenteditable" div.Heres a fiddle, and heres the function:function getWordPrec…

Javascript - strikethrough

i try to make text strikethrough with javascript. I know nothing about Javascript and try to search on the net how to that.<script language="JavaScript">function recal(val,sum){ if(sum …

JavaScript loop/scope issue with Image.onload()

Im trying to loop through an object with JavaScript and add all the subobjects from that object to a HTML5 canvas.The canvas-bit is working, no problem with that, but for some reason all my images end …