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 :: react js onclick call two functions 
Javascript :: array to string javascript 
Javascript :: Nodemailer gmail new configuration 
Javascript :: jquery on click outsile hide div 
Javascript :: typescript read json file 
Javascript :: merge 2 dictionaries with same keys javascript 
Javascript :: how to remove timezone from date in javascript 
Javascript :: filter array inside array 
Javascript :: javascript array add 
Javascript :: import javasciprt module dynamically 
Javascript :: javascript shuffle string 
Javascript :: Javascript how to differentiate single click event and double click event 
Javascript :: js change value of every value in an object 
Javascript :: get table row data jquery 
Javascript :: jquery click not working on dynamic content 
Javascript :: jquery telephone input mask 
Javascript :: mongoose schema cast decimals 
Javascript :: disable a button react 
Javascript :: transpose of the matrix in javascript 
Javascript :: Unterminated string constant. 
Javascript :: jquery add event after page load 
Javascript :: what is cdn react 
Javascript :: sum elements in list with same name js 
Javascript :: remove line break javascript 
Javascript :: retrieve data from option select js 
Javascript :: jquery addclass 
Javascript :: node server 
Javascript :: iterate through array js 
Javascript :: filereader check file type 
Javascript :: react-fragment 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =