
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Fetch Second Minimum Element from an Array without Sorting in JavaScript
We have an array of Numbers, and we are required to write a function that returns the second smallest value from the array.
For example − if the array is −
const arr = [67, 87, 56, 8, 56, 78, 54, 67, 98, 56, 54];
Then the output should be the following −
54
because 54 is the smallest value after 8
Example
const arr = [67, 87, 56, 8, 56, 78, 54, 67, 98, 56, 54]; const minimumIndex = arr => { return arr.indexOf(Math.min(...arr)); }; const secondMinimum = arr => { const copy = arr.slice(); copy.splice(minimumIndex(copy), 1); return copy[minimumIndex(copy)]; }; console.log(secondMinimum(arr));
Output
The output in the console will be −
54
Advertisements