finding the median in an array

Solutions on MaxInterview for finding the median in an array by the best coders in the world

showing results for - "finding the median in an array"
Emilia
25 Sep 2016
1//#Source https://bit.ly/2neWfJ2 
2const median = arr => {
3  const mid = Math.floor(arr.length / 2),
4    nums = [...arr].sort((a, b) => a - b);
5  return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2;
6};
7console.log(median([5, 6, 50, 1, -5]));
8console.log(median([1, 2, 3, 4, 5]));
9
10