Find Second Smallest Element in an Array in JavaScript Last Updated : 14 Mar, 2024 Comments Improve Suggest changes Like Article Like Report JavaScript allows us to compute the Second Smallest Element within a given array. The task involves iterating through the array, identifying the smallest and second smallest element in the array. There are several approaches to finding the second smallest element in an array using Javascript which are as follows: Table of Content Using Sort method()Using iterationUsing Math.min and filter() Using Sort method()In this approach, we sort the array in ascending order using the sort method and return the second element which is the second smallest element in the array. Example: The below code Finds the Second Smallest Element in an Array using sorting in JavaScript. JavaScript function secondSmallestElement(arr) { arr.sort((a, b) => a - b); return arr[1]; } const numbers = [5, 2, 8, 3, 1, 7]; console.log("Second smallest element:", secondSmallestElement(numbers)); OutputSecond smallest element: 2 Using iterationIn this approach, we iterate through the array to find the smallest and second smallest elements simultaneously. We maintain two variables to store the current smallest and second smallest elements while scanning the array. Example: The below code Find the Second Smallest Element in an Array using linear scan in JavaScript. JavaScript function secondSmallestElement(arr) { let smallest = Infinity; let secondSmallest = Infinity; for (let i = 0; i < arr.length; i++) { if (arr[i] < smallest) { secondSmallest = smallest; smallest = arr[i]; } else if (arr[i] < secondSmallest && arr[i] !== smallest) { secondSmallest = arr[i]; } } return secondSmallest; } const numbers = [5, 2, 8, 3, 1, 7]; console.log("Second smallest element:", secondSmallestElement(numbers)); OutputSecond smallest element: 2 Using Math.min and filter()Find the minimum value in the array using Math.min and exclude the smallest element using filter method. Example: This JavaScript code uses filtering to exclude the smallest element and then finds the minimum of the remaining array, effectively determining the second smallest element. JavaScript function secondSmallest(arr) { const smallest = Math.min(...arr); const filteredArray = arr .filter(num => num !== smallest); return Math.min(...filteredArray); } console.log(secondSmallest([1, 12, 13, 2,])) Output2 Comment More infoAdvertise with us Next Article Find Second Smallest Element in an Array in JavaScript A anjugaeu01 Follow Improve Article Tags : JavaScript Web Technologies Similar Reads JavaScript Program to Find kth Largest/Smallest Element in an Array JavaScript allows us to find kth largest/smallest element in an array. We are given an array containing some elements, we have to find kth smallest/largest element from the array where k is a number greater than zero and less than equal to the total number of elements present in the array. There are 5 min read JavaScript - Max Element in an Array These are the following ways to find the largest element in JS array: Note: You must add a conditional check for checking whether the given array is empty or not, if the array is not empty then you can use the given below approaches to find out the largest element among them. 1. Using the Spread Ope 4 min read JavaScript - Find Index of a Value in Array Here are some effective methods to find the array index with a value in JavaScript.Using indexOf() - Most Used indexOf() returns the first index of a specified value in an array, or -1 if the value is not found. JavaScriptconst a = [10, 20, 30, 40, 50]; // Find index of value 30 const index = a.inde 2 min read JavaScript Program to Find Largest Element in an Array In this article, we are going to learn about the largest element in an array in JavaScript. The largest element in an array refers to the value that holds the greatest numerical or lexicographic (string) order among all elements present in the array. Example: Input : [10, 15, 38, 20, 13];Output: 38H 3 min read JavaScript â Min Element in an Array These are the following ways to find the minimum element in JS array:Note: You must add a conditional check for checking whether the given array is empty or not, if the array is not empty then you can use the given below approaches to find out the minimum element among them.1. Using Math.min() Metho 3 min read Find Kth Element of Two Sorted Arrays in JavaScript Given two sorted arrays, our task is to find the Kth element in the combined array made by merging the two input arrays in JavaScript.Example:Input: Arr1: [1, 2, 5] , Arr2:[2, 4, 6, 8], K = 4Output: Kth element is 4.Explanation:The final Array would be: [1, 2, 2, 4, 5, 6, 8]The 4th element of this a 3 min read Javascript Program for Least frequent element in an array Given an array, find the least frequent element in it. If there are multiple elements that appear least number of times, print any one of them.Examples : Input : arr[] = {1, 3, 2, 1, 2, 2, 3, 1}Output : 33 appears minimum number of times in givenarray.Input : arr[] = {10, 20, 30}Output : 10 or 20 or 3 min read Find the min/max element of an Array using JavaScript To find the minimum or maximum element in a JavaScript array, use Math.min or Math.max with the spread operator.JavaScript offers several methods to achieve this, each with its advantages.Using Math.min() and Math.max() Methods The Math object's Math.min() and Math.max() methods are static methods t 2 min read How to Get Index of Greatest Value in an Array in JavaScript ? In this article, we will see how to return the index for the greatest value in an array with the help of JavaScript. To return the index of the greatest value in an array using JavaScript.Some of the examples are to return the index of the greatest value in an array.Example: The greatest value in th 4 min read JavaScript Program to find the Nth smallest/largest element from an unsorted Array In this article, we will see how to find Nth largest and smallest element from an unsorted array. The Nth smallest/largest element from an unsorted array refers to the element that ranks at the Nth position when the array is sorted either in ascending (smallest) or descending (largest) order. It rep 4 min read Like