Open In App

How to get median of an array of numbers in JavaScript ?

Last Updated : 31 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will see how to find the median of an array using JavaScript. A median is a middle value in a given set of numbers or data.

Examples:

Input arr1 : [1 4 7 9]
Output : 5.5
Explanation : The median of the two sorted array is
the middle elements which is 5 if the
arrayLength =arr1.length + arr2.length is odd
if the array length is even then it will be
the average of two number

Input: arr1 : [5 3 1 8 90]

Output: 5
Explanation : The median is average of two number
because the array.length is even for this case.

Approach 1: Here we first sort the array and then will find the array length. If the array length is even then median will be arr[(arr.length)/2] +arr[((arr.length)/2)+1]. If the array length is odd then the median will be a middle element.

Example:

JavaScript
<script>
    function medianof2Arr(arr1) {
        var concat = arr1;
        concat = concat.sort(
            function (a, b) { return a - b });
    
        console.log(concat);
        var length = concat.length;
    
        if (length % 2 == 1) {
    
            // If length is odd
            console.log(concat[(length / 2) - .5])
            return concat[(length / 2) - .5]
    
        }
        else {
            console.log((concat[length / 2] 
                + concat[(length / 2) - 1]) / 2);
                
            return (concat[length / 2] 
                + concat[(length / 2) - 1]) / 2;
        }
    }
    
    arr1 = [1, 4, 7, 9]
    
    medianof2Arr(arr1)
</script>

 Output:

5.5

Approach 2: Here we have first made the variable middle which has the middle value irrespective of length whether the array length will be odd or even. Now after that, we sort the array by avoiding mutation. Mutation means changing the object name with another object name or passing the object to another object called a mutation.

It can be done with reference data types which are arrays, Object So For now avoid this. After that, if the length of the array is even then we have the two values in the array at pos arr((arr.length)/2) + arr(((arr.length)/2) +1). Then take the mean of these two numbers and return as a median.

Example:

JavaScript
<script>
    function median_of_arr(arr) {
        const middle = (arr.length + 1) / 2;
    
        // Avoid mutating when sorting
        const sorted = [...arr].sort((a, b) => a - b);
        const isEven = sorted.length % 2 === 0;
    
        return isEven ? (sorted[middle - 1.5]
            + sorted[middle - 0.5]) / 2 :
            sorted[middle - 1];
    }
    var arr = [1, 4, 7, 9];
    console.log(median_of_arr(arr));
</script>

Output: 

5.5

Approach 3: Here, we will be using the Math library and Array slicing. In this approach, we first sort the array in ascending order. Then, we check if the length of the array is even or odd. If it’s even, we take the average of the middle two elements. If it’s odd, we directly return the middle element.

Example:

JavaScript
function median(arr) {
    // Sort the array
    arr.sort((a, b) => a - b);

    const length = arr.length;
    const middle = Math.floor(length / 2);

    // Check if the array length is even or odd
    if (length % 2 === 0) {
        // If even, return the average of middle two elements
        return (arr[middle - 1] + arr[middle]) / 2;
    } else {
        // If odd, return the middle element
        return arr[middle];
    }
}

const arr1 = [1, 4, 7, 9];
console.log(median(arr1)); // Output: 5.5

const arr2 = [5, 3, 1, 8, 90];
console.log(median(arr2)); // Output: 5

Output
5.5
5




Next Article

Similar Reads