Program to find mean of array after removing some elements in Python

Suppose we have an array called nums, we have to find the mean of the remaining values after removing the smallest 5% and the largest 5% of the elements.

So, if the input is like nums = [2,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,8], then the output will be 4.0 because after removing smallest and largest values, all remaining elements are the same.

Algorithm

To solve this problem, we will follow these steps ?

  • Sort the array nums

  • n := size of nums

  • per := quotient of (n * 5 / 100)

  • trimmed_array := subarray of nums from index per to (n - per - 1)

  • mean := average of all elements in trimmed_array

  • Return mean

Example

Let us see the following implementation to get better understanding ?

def solve(nums):
    nums.sort()
    
    n = len(nums)
    per = int(n * 5 / 100)
    trimmed_array = nums[per : n - per]
    
    mean = sum(trimmed_array) / len(trimmed_array)
    
    return mean

nums = [2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8]
print("Original array:", nums)
print("Mean after removing 5% from each end:", solve(nums))
Original array: [2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8]
Mean after removing 5% from each end: 4.0

How It Works

Let's trace through the example step by step ?

  1. Original array: [2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8] (20 elements)

  2. After sorting: Already sorted in this case

  3. Calculate 5%: per = int(20 * 5 / 100) = 1

  4. Remove elements: Remove first 1 element (2) and last 1 element (8)

  5. Trimmed array: [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4] (18 elements)

  6. Calculate mean: sum(18 fours) / 18 = 72 / 18 = 4.0

Alternative Implementation

Here's a more concise version using Python's statistics module ?

import statistics

def solve_alternative(nums):
    nums.sort()
    n = len(nums)
    per = int(n * 5 / 100)
    trimmed_array = nums[per : n - per]
    return statistics.mean(trimmed_array)

nums = [6, 2, 7, 5, 1, 2, 0, 3, 10, 2, 5, 0, 5, 5, 0, 8, 7, 6, 8, 0]
print("Mean after trimming:", solve_alternative(nums))
Mean after trimming: 4.0

Conclusion

This trimmed mean approach removes outliers by discarding the extreme 5% values from both ends. It's useful for getting a more robust average that's less affected by outliers than the regular arithmetic mean.

Updated on: 2026-03-25T20:23:58+05:30

322 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements