-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmergeArrays.js
26 lines (19 loc) · 1019 Bytes
/
mergeArrays.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
You are given two sorted arrays that contain only integers. Your task is to find a way to merge them into a single one, sorted in ascending order. Complete the function mergeArrays(arr1, arr2), where arr1 and arr2 are the original sorted arrays.
You don't need to worry about validation, since arr1 and arr2 must be arrays with 0 or more Integers. If both arr1 and arr2 are empty, then just return an empty array.
Note: arr1 and arr2 may be sorted in different orders. Also arr1 and arr2 may have same integers. Remove duplicated in the returned result.
Examples
arr1 = [1, 2, 3, 4, 5];
arr2 = [6, 7, 8, 9, 10];
mergeArrays(arr1, arr2); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
arr3 = [1, 3, 5, 7, 9];
arr4 = [10, 8, 6, 4, 2];
mergeArrays(arr3, arr4); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
arr5 = [1, 3, 5, 7, 9, 11, 12];
arr6 = [1, 2, 3, 4, 5, 10, 12];
mergeArrays(arr5, arr6); // [1, 2, 3, 4, 5, 7, 9, 10, 11, 12]
*/
//Answer//
function mergeArrays(a1, a2) {
return [...new Set(a1.concat(a2).sort((a,b)=>a-b))]
}