DEV Community

Cole Rau
Cole Rau

Posted on • Edited on

How to find the median and mean of an array in Ruby

Finding the Median

The Code:

array = [172, 185, 160, 230, 50, 185, 190]

sorted_array = array.sort 
count = sorted_array.count 

if sorted_array.count % 2 == 0 
  first_half = (sorted_array[0...(count/2)])
  second_half = (sorted_array[(count/2)..-1])

  first_median = first_half[-1]
  second_median = second_half[0]

  true_median = ((first_median + second_median).to_f / 2.to_f)
  true_median
else 
  true_median = sorted_array[(count/2).floor]
  true_median
end 
Enter fullscreen mode Exit fullscreen mode

Code Explanation:

The median is the middle number of an array sorted from lowest to highest number. We begin with an array. We then sort the array from lowest to highest number with .sort. Then we get the amount of elements in the array with .count. Next we set up a conditional if, else statement: If the sorted array has an even amount of numbers in it, we execute the code after the if. If the sorted array has an odd amount of numbers, we execute the code after the else.

(The code if sorted_array.count % 2 == 0 is saying, "If the remainder of the amount of elements in the sorted array divided by 2 is 0, then execute the code below the if statement." The % operator in Ruby returns the remainder after dividing the number to the left of the % by the number to the right. We perform the operation sorted_array.count % 2 == 0 to check if the array has an even amount of numbers because all even numbers, when divided by 2, will have a remainder of 0).

Assuming the array has an even amount of numbers, we first split up the array into two parts: The first_half is the sorted array from the first element to the element before the element at sorted_array[(count/2)]. (The ... here means exclude the second argument but leave all the stuff in before it). The second_half is the sorted array from the element at sorted_array[(count/2)] to the last element. (Here the .. means include the second argument; the -1 means the last element of the array). Next, the variable first_median is the last element of the array found in the first_half variable. The variable second_median is the first element of the array found in the second_half variable. Now, we need to take the average of our two elements to receive the actual median value. We do this with true_median = ((first_median + second_median).to_f / 2.to_f). The .to_f is important to have because without it Ruby will do integer division and you may end up with a rounded, imprecise answer. Finally, we return the value of true_median.

Assuming the array has an odd amount of numbers, we can find the median by taking the sorted array and finding the element at (count/2).floor. The .floor rounds down to the nearest integer and is essential to get the right answer. Finally, we return the value of true_median.


Finding the Mean (Average)

The Code:

array = [172, 185, 160, 230, 50, 185, 190]

numerator = array.reduce(0) { |a, v| a + v }
denominator = array.count
mean = numerator.to_f / denominator.to_f 
mean
Enter fullscreen mode Exit fullscreen mode

Code Explanation:

The mean (or average) is calculated by adding up all the numbers and then dividing by the amount of numbers.

We begin by using the reduce enumerator, which goes through each element of the array, adding each element to an accumulator. The (0) is the starting value of the accumulator in this case. Each element of the array gets added to 0. The a stands for accumulator and the v stands for value (or element of the array). The reduce enumerator lets us find the numerator of the mathematical expression for the mean.

To find the denominator, we get the amount of numbers in the array with .count.

Finally, we convert the numerator and denominator into floats with .to_f and divide the numerator by the denominator. Finally, we return the mean variable.


Let me know in the comments if you have a different/faster/simpler way to calculate the mean and median!

Top comments (0)