Finding the Median of Two Sorted Arrays with JavaScript

The "Find the median of two sorted arrays" implementation shown here combines the two inputs, sorts the values, and reads the middle element or pair. It is straightforward and easy to follow, but it does not exploit the existing ordering optimally. A logarithmic binary‑partition solution is mentioned later as an alternative; it is not the implementation demonstrated in this article.
Problem Description
Given two sorted arrays, nums1 and nums2, find the median of the combined array.
Example
// Input:
nums1 = [1, 3];
nums2 = [2];
// Output: 2.0
// Input:
nums1 = [1, 2];
nums2 = [3, 4];
// Output: 2.5
// (Because there are an equal number of numbers, so we add the middle two: (2 + 3) / 2 = 2.5)Solution Approach
Merge the two arrays:
Begin by merging the two sorted arrays to form a single sorted array.Find the median:
- If the length of the merged array is odd, return the middle element.
- If the length is even, return the average of the two middle elements.
Solving with JavaScript (TypeScript)
const findMedianSortedArrays = (nums1: number[], nums2: number[]): number => {
const merged = [...nums1, ...nums2].sort((a, b) => a - b);
const mid = Math.floor(merged.length / 2);
if (merged.length % 2 === 0) {
return (merged[mid - 1] + merged[mid]) / 2;
}
return merged[mid];
};Why is This Problem Important?
Whilst sometimes 'leet' developers can be accused of making up problems simply for the sake of their complexity, there are some real values in the Two Sorted Arrays problem, and seeing how a developer approaches a solution:
Fundamentals
: It solidifies and demonstrates an understanding of arrays, sorting, and mathematical calculations.Performance
: Finding an efficient solution (beyond the one presented above) requires diving deep into the properties of sorted arrays, which can lead toO(log(min(m,n)))solutions using binary search.Problem‑solving skills:
The problem presents an opportunity to think about edge cases, different data scenarios, and efficient algorithms.
About the Expected Logarithmic Solution
The straightforward approach is to merge the two sorted arrays and then read the middle value. That is easy to understand and perfectly reasonable in ordinary application code when the arrays are small. In an interview setting, though, this problem is usually asked because there is a more constrained O(log(m + n)) solution.
That faster approach uses binary search to find a partition point where the left side of the combined values contains everything that should sit before the median. It is more complex to explain and easier to get wrong, so I would not reach for it unless the input size or the exercise requires it.
Edge Cases Worth Testing
Test one empty array, arrays of different lengths, duplicate values, negative numbers, and even versus odd combined lengths. Those cases prove whether the median calculation is correct rather than merely working for the tidy example.
The Wrap‑up
The code demonstrated here combines and sorts all m + n values before selecting the median, so its complexity is normally O((m + n) log(m + n)). A binary‑partition approach can solve the problem in O(log(min(m, n))) time, but that is an unimplemented alternative in this article rather than the solution just demonstrated.