Calculate Median Value
ARRAY
MATH

Problem

Given an array of integers numbers, return the median value.

For an array with an odd number of elements, the median is the middle element when the array is sorted.

For an array with an even number of elements, the median is the lower of the two middle elements when the array is sorted.

You do not need to round the median value since it will always be an integer based on these rules.

Examples

calculateMedian([1, 2, 3, 4, 5]) // 3 // The median value of the array [1, 2, 3, 4, 5] is 3. calculateMedian([3, 4, 1, 2]) // 2 /* The median value of the array [3, 4, 1, 2] is 2. Since the array length is even, we pick the lower middle element after the array is sorted. */ calculateMedian([10,25,15,20,30,40]) // 20
Loading...