

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Third smallest number in an array using JavaScript
Problem
We are required to write a JavaScript function that takes in an array of numbers of length at least three.
Our function should simply return the third smallest number from the array.
Example
Following is the code −
const arr = [6, 7, 3, 8, 2, 9, 4, 5]; const thirdSmallest = () => { const copy = arr.slice(); for(let i = 0; i < 2; i++){ const minIndex = copy.indexOf(Math.min(...copy)); copy.splice(minIndex, 1); }; return Math.min(...copy); }; console.log(thirdSmallest(arr));
Output
4
- Related Questions & Answers
- JavaScript Finding the third maximum number in an array
- Finding the third maximum number within an array in JavaScript
- Remove smallest number in Array JavaScript
- Finding smallest number using recursion in JavaScript
- Finding the largest and smallest number in an unsorted array of integers in JavaScript
- Get the smallest array from an array of arrays in JavaScript
- Java program to find the smallest number in an array
- Checking digit sum of smallest number in the array in JavaScript
- Smallest Common Multiple of an array of numbers in JavaScript
- Rearrange An Array In Order – Smallest, Largest, 2nd Smallest, 2nd Largest,. Using C++
- Java program to find the 2nd smallest number in an array
- JavaScript Recursion finding the smallest number?
- Finding the smallest fitting number in JavaScript
- Third Maximum Number in C++
- Find the difference of largest and the smallest number in an array without sorting it in JavaScript
Advertisements