Source: https://leetcode.com/problems/median-of-two-sorted-arrays/

Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number}
*/
// Linear mergesortish approach
// O(N + M) time, O(1) space
var findMedianSortedArrays = function(nums1, nums2) {
let median1 = 0;
let median2 = 0;
let index1 = 0;
let index2 = 0;
let i = 0;
// For the combined array, we go up to the halfway point
while (i <= Math.floor((nums1.length + nums2.length) / 2)) {
median1 = median2;
// We've exhaused all of nums1, so move nums2 pointer
if (index1 === nums1.length) {
median2 = nums2[index2];
index2++;
// We've exhausted all of nums2, so move nums1 pointer
} else if (index2 === nums2.length) {
median2 = nums1[index1];
index1++;
// If the smaller number is from nums1, we move nums1 pointer
} else if (nums1[index1] <= nums2[index2]) {
median2 = nums1[index1];
index1++;
// If the smaller number is from nums2, we move nums2 pointer
} else {
median2 = nums2[index2];
index2++;
}
i++;
}
// If the combined array has an even length, we do median1 + median2 / 2
if ((nums1.length + nums2.length) % 2 === 0) {
return (median1 + median2) / 2
}
// If the combined array has an odd length, we get the exact middle
return median2;
};