Skip to content

Commit

Permalink
Add merge sorted array func to utils
Browse files Browse the repository at this point in the history
Signed-off-by: Darko Draskovic <[email protected]>
  • Loading branch information
darkodraskovic committed Jan 29, 2021
1 parent 37e622e commit 12940cc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// import { MyArray } from './array.js';
import { doTwoMemSumTo } from './utils.js'
import { reverseStringLambda } from './utils.js'
import { mergeSorted } from './utils.js'

// const arr1 = ['a', 'b', 'c', 'd', 'e'];
// const arr2 = ['x', 'y', 'a', 'z', '0'];
// console.log(arr1.some(item => arr2.includes(item)));
// console.log(containCommonElement(arr1, arr2));
// console.log(doTwoMemSumTo(12, [1, 3, 9, 7, 7, 1, 10, 5, 2]));
// console.log(reverseStringLambda('abac'));

console.log(doTwoMemSumTo(12, [1, 3, 9, 7, 7, 1, 10, 5, 2]));
console.log(reverseStringLambda('abac'));
let a = [0, 3, 4, 31];
let b = [4, 6, 30];
console.log(mergeSorted(a, b));
14 changes: 14 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,17 @@ export function doTwoMemSumTo(n, arr) {
}
return false;
}

export function mergeSorted(a, b) {
if (a.length === 0) return b;
if (b.length === 0) return a;

const c = [];
let ai = 0;
let bi = 0;
while(ai < a.length && bi <b.length) {
if (a[ai] < b[bi]) c.push(a[ai++]);
else c.push(b[bi++]);
}
return c.concat(ai < a.length ? b.slice(bi) : a.slice(ai));
}

0 comments on commit 12940cc

Please sign in to comment.