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
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 ]
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.
Example
function removeMinMax(arr) {
let min = Math.min(...arr);
let max = Math.max(...arr);
while (arr.includes(min) || arr.includes(max)) {
arr.splice(arr.indexOf(min), 1);
arr.splice(arr.indexOf(max), 1);
}
return arr;
}
let array = [1, 5, 3, 7, 2, 9];
removeMinMax(array);
console.log(array); // [5, 3, 7, 2]
Output
[ 5, 3, 7, 2 ]
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.
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 ]
Approach 7: Using a Set to Remove Elements
In this approach, we will use a Set to remove the smallest and largest elements from the array. We first identify the smallest and largest elements and then use the Set object to store only the elements that are not the smallest or largest. Finally, we convert the Set back to an array.
Example: Using a Set to Remove Elements
// Using Set
let inputArray = [4, 7, 2, 9, 1, 8, 3, 6, 5];
let smallestElement = Math.min(...inputArray);
let largestElement = Math.max(...inputArray);
let resultSet = new Set(inputArray);
resultSet.delete(smallestElement);
resultSet.delete(largestElement);
let resultArray = Array.from(resultSet);
console.log(resultArray);
Output
[ 4, 7, 2, 8, 3, 6, 5 ]