Open In App

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

In this article, 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:



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

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.



Example: In this example, we are using reduce() method in JavaScript.




// Using reduce() method 
let inputArray = [3, 1, 7, 9, 2, 8, 4, 6, 5];
let smallestElement = Math.min(...inputArray);
let largestElement = Math.max(...inputArray);
inputArray =
    inputArray.reduce((newArray, currentElement) => {
        if (currentElement !== smallestElement &&
            currentElement !== largestElement) {
            newArray.push(currentElement);
        }
        return newArray;
    }, [])
  
console.log(inputArray);

Output
[
  3, 7, 2, 8,
  4, 6, 5
]

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.

Syntax:

 for (let i = 0; i < inputArray.length; i++) {
// Code..
}

Example: In this example, we are using the iteration loop (for) in JavaScript




//Using For loop
let inputArray = [10, 45, 78, 23, 44, 11, 67];
let smallestElement = Math.min(...inputArray);
let largestElement = Math.max(...inputArray);
let newArray = [];
  
for (let i = 0; i < inputArray.length; i++) {
    if (inputArray[i] !== smallestElement &&
        inputArray[i] !== largestElement) {
        newArray.push(inputArray[i]);
    }
}
  
console.log(newArray);

Output
[ 45, 23, 44, 11, 67 ]

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.

Syntax:

for (let item of inputArray) {
// Code . . .
}
let newArray = inputArray.filter();

Example: In this example, we are using the above-explained approach.




// Without using Math.min() and Math.max()
let inputArray = [10, 5, 4, 6, 8, 1, 3, 9, 2];
let smallestElement = Infinity;
let largestElement = -Infinity;
  
for (let item of inputArray) {
    if (item < smallestElement) smallestElement = item;
    if (item > largestElement) largestElement = item;
}
  
let newArray =
    inputArray.filter(
        num => num !== smallestElement && num !== largestElement);
  
console.log(newArray);

Output
[
  5, 4, 6, 8,
  3, 9, 2
]

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.

Syntax:

function remove(arr) {
arr.sort((a, b) => a - b);
arr = arr.slice(1, arr.length - 1);
return arr;
}

Example: In this example, we are using the above-explained approach.




function remove(arr) {
    arr.sort((a, b) => a - b);
    arr = arr.slice(1, arr.length - 1);
    return arr;
}
  
const newArray = [10, 5, 4, 6, 8, 1, 3, 9, 2];
const result = remove(newArray);
console.log(result);

Output
[
  2, 3, 4, 5,
  6, 8, 9
]

Article Tags :