An algorithm to merge two different arrays alternatively in to third array in javascript.
Example
Input: [1, 2, 3, 4, 5, 6, 7 , 8] [11, 22, 33, 44] Output: [1, 11, 2, 22, 3, 33, 4, 44, 5, 6, 7, 8]
A simple brute force approach to alternatively merge two arrays.
Implementation
- We will copy each element from the both the arrays alternatively in the the third array till there is element in any one of them.
- Then we will check if there is still element in any of the arrays then add them to the third array.
let alternateMerge = (arr1, arr2, n1 = arr1.length, n2 = arr2.length) => { let i = 0, j = 0; let temp = []; //Add each element from both the array while(i < n1 && j < n2){ temp.push(arr1[i], arr2[j]); i++; j++; } //If there is still element in first array //then add them while(i < n1){ temp.push(arr1[i]); i++; } //If there is still element in second array //then add them while(j < n2){ temp.push(arr2[j]); j++; } return temp; }
Input: console.log(alternateMerge([1, 2, 3, 4, 5, 6, 7 , 8],[11, 22, 33, 44])); Output: [1, 11, 2, 22, 3, 33, 4, 44, 5, 6, 7, 8]
Time complexity: O(n1 + n2).
Space complexity: O(n1 + n2).