- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove smallest number in Array JavaScript
We are required to write a JavaScript function that takes in an array of numbers. The number should find the smallest element in the array and remove it in place.
The code for this will be −
const arr = [2, 1, 3, 2, 4, 5, 1]; const removeSmallest = arr => { const smallestCreds = arr.reduce((acc, val, index) => { let { num, ind } = acc; if(val >= num){ return acc; }; ind = index; num = val; return { ind, num }; }, { num: Infinity, ind: -1 }); const { ind } = smallestCreds; if(ind === -1){ return; }; arr.splice(ind, 1); }; removeSmallest(arr); console.log(arr);
Following is the output on console −
[ 2, 3, 2, 4, 5, 1 ]
- Related Articles
- Third smallest number in an array using JavaScript
- Checking digit sum of smallest number in the array in JavaScript
- How to remove certain number elements from an array in JavaScript
- Finding the largest and smallest number in an unsorted array of integers in JavaScript
- Remove number from array and shift the remaining ones JavaScript
- Finding smallest number using recursion in JavaScript
- Finding the smallest fitting number in JavaScript
- Find the smallest number in a Java array.
- JavaScript Recursion finding the smallest number?
- Remove leading zeros in array - JavaScript
- Smallest number after removing n digits in JavaScript
- Get the smallest array from an array of arrays in JavaScript
- Find the 3rd smallest number in a Java array.
- Find the 2nd smallest number in a Java array.
- Remove leading zeros in array in JavaScript

Advertisements