Skip to content

Commit e2fe686

Browse files
authored
Merge pull request vJechsmayr#81 from psalmfill/al-median-of-two-sorted-array
Algorithm for Finding Median of Two Sorted Array
2 parents 88c4d88 + 20445bd commit e2fe686

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const findMedianSortedArrays = function (nums1, nums2) {
2+
const joined_array = [...nums1, ...nums2].sort();
3+
const total = joined_array.length;
4+
//check if array is even in length
5+
if (total % 2 == 0) {
6+
let first_bound = (total / 2) - 1;
7+
let second_bound = first_bound + 1;
8+
return (joined_array[first_bound] + joined_array[second_bound]) / 2;
9+
}
10+
let position = ((total - 1) / 2) + 1;
11+
return joined_array[position];
12+
};
13+
module.exports = findMedianSortedArrays;

0 commit comments

Comments
 (0)