Find Second Smallest Element in an Array in JavaScript Last Updated : 14 Mar, 2024 Summarize Comments Improve Suggest changes Share 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 JavaScript Program to Find Largest Element in an Array 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 Like