Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to merge two sorted arrays in javascript

// using for single for loop 

let arrrone = [0,3,4,31];
let arrayTwo = [4,6,30,31,33];
let j = 0;
let k = 0;
let mergedArray = [];

let length = arrrone.length + arrayTwo.length;

console.log(length)

for(let i = 0; i<= length - 1; i++){
  if(arrrone[j] < arrayTwo[k]){
    mergedArray.push(arrrone[j]);
    j++;
  }
  else if(arrrone[j] > arrayTwo[k]){
    mergedArray.push(arrayTwo[k]);
    k++;
  }
  else if(arrrone[j] == arrayTwo[k]){
    mergedArray.push(arrrone[j]);
    mergedArray.push(arrayTwo[k]);
    j++;
    k++;
  }
}

console.log(mergedArray)
Comment

javascript merge two sorted arrays

let mergedArray = [...arr1, ...arr2];
return mergedArray.sort((a, b) => a-b);
Comment

javascript merge two sorted arrays

let mergedArr = [...arr1, ...arr2];
return mergedArr.sort((a, b) => a-b);
Comment

PREVIOUS NEXT
Code Example
Javascript :: images node backend server 
Javascript :: how to count click events javascript 
Javascript :: json parse vs json stringify 
Javascript :: favicon express js 
Javascript :: change the focus to next in angular forms 
Javascript :: javascript console log current directory 
Javascript :: range between two numbers 
Javascript :: share data between livewire and alpine js 
Javascript :: how to defined an array in js 
Javascript :: js how to fix 0.1 + 0.2 
Javascript :: javascript make pong 
Javascript :: does javascript buelt applications 
Javascript :: node js write read string to file 
Python :: python most used functions 
Python :: francais a anglais 
Python :: get wd in python 
Python :: remove all pyc files 
Python :: legend size matplotlib 
Python :: conda install lxml 
Python :: '.join([chr((ord(flag[i]) &lt;&lt; 8) + ord(flag[i + 1])) for i in range(0, len(flag), 2)]) 
Python :: jupyter notebook print all rows dataframe 
Python :: python delay 
Python :: bytes to string python 
Python :: python format seconds to hh mm ss 
Python :: gdscript string format 
Python :: plt.imshow grayscale 
Python :: django return httpresponse 
Python :: read multiple csv python 
Python :: python - prime number generator 
Python :: python how to write pandas dataframe as tsv file 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =