How to useArray.filter() and Math.min/Math.max() in Javascript

This approach combines the use of the Array.filter() method with Math.min/Math.max() to remove the smallest and largest elements from the array.

JavaScript
function removeMinMax(arr) {
    // Find the minimum and maximum values in the array
    let min = Math.min(...arr);
    let max = Math.max(...arr);
    
    // Filter out the elements that are equal to the minimum or maximum values
    return arr.filter(num => num !== min && num !== max);
}

// Example usage
let array = [1, 5, 3, 7, 2, 9];
let result = removeMinMax(array);
console.log(result); // Output: [5, 3, 7, 2]

Output
[ 5, 3, 7, 2 ]




How to Remove Smallest and Largest Elements from an Array in JavaScript ?

We will cover how to remove the smallest and largest elements from an array in JavaScript. Removing the smallest and largest elements from an array in JavaScript means filtering out the minimum and maximum values from the original array, leaving only the intermediate elements. We will see the code for each approach along with the output.

There are several methods that can be used to remove the smallest and largest elements from an array in JavaScript, which are listed below:

Table of Content

  • Using the reduce() Method
  • Using the iteration loop (for)
  • Using the Loop and filter() Method
  • Using Array.sort() and Array.slice() Methods
  • Using Array.splice() and Array.includes()
  • Using Array.filter() and Math.min/Math.max()


We will explore all the above methods along with their basic implementation with the help of examples.

Similar Reads

Approach 1: Using the reduce() Method

Here, this approach uses the reduce() method in JavaScript where we will iterate through the inputted array and find the largest and smallest element. Then we will maintain a result array in which we will append or store only the elements that do not match the largest and smallest element conditions specified in the if condition....

Approach 2: Using the iteration loop (for)

Here, in this approach, we will use the for loop for removing the smallest and largest element from the given array. We will compare each element with the min and max elements and we will store the newly created array in another array variable....

Approach 3: Using the Loop and filter() Method

Here, in this approach, we create a new array by using the filter() function in JavaScript which actually excludes the smallest and largest elements and results in the removal of these values from the original inputted array in the code itself....

Approach 4: Using Array.sort() and Array.slice() Methods

To remove the smallest and largest elements from an array, use array.sort() to sort in ascending order, then array.slice(1, -1) to remove the first and last elements....

Approach 5: Using Array.splice() and Array.includes()

Using `Array.splice()` with `Array.includes()`, the function repeatedly removes the smallest and largest elements from the array until neither is present. This approach directly manipulates the array based on element inclusion checks....

Method 6: Using Array.filter() and Math.min/Math.max()

This approach combines the use of the Array.filter() method with Math.min/Math.max() to remove the smallest and largest elements from the array....