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:
Using the reduce() Method
Using the Iteration Loop (for)
Using the Loop and filter() Method
Using Array sort() and Array slice() Methods
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.
Javascript
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
]
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
Javascript
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 ]
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.
Javascript
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
]
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.
Javascript
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
]
Learn to code easily with our course
Coding for Everyone . This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
01 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...