JavaScript – Min Element in an Array Last Updated : 19 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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() Method (Efficient for Small Arrays)This method uses the Math.min() function to find the smallest value in the array by spreading the elements as individual arguments. It’s simple and concise but can be less efficient for large arrays due to the spread operator's overhead. Note: The min() function returns “Infinity” for the empty array. JavaScript const a = [5, 12, 8, 130, 44]; const res = Math.min(...a); console.log(res); Output5 2. Using for Loop (Simple & Efficient) The for loop iterates through the array, updating the smallest value (res) whenever a smaller element is found. This approach is efficient, with O(n) time complexity, and is ideal for performance-sensitive situations. JavaScript const a = [5, 12, 8, 130, 44]; let res = a[0]; for (let i = 1; i < a.length; i++) { if (a[i] < res) { res = a[i]; } } console.log(res); Output5 3. Using reduce() Method (Efficient for Large Arrays)The reduce() method processes each element of the array, comparing it with an accumulator (res) to track the smallest value. It’s functional and concise, with O(n) time complexity, and is great for functional programming enthusiasts. JavaScript const a = [5, 12, 8, 130, 44]; const res = a.reduce((min, c) => (c < min ? c : min), a[0]); console.log(res); Output5 4. Using forEach() Method (Simple but slightly less efficient)The forEach() method iterates over the array and updates res if a smaller element is found. While simple, it’s slightly less efficient than the for loop due to the overhead of invoking a callback function. Time complexity: O(n). JavaScript const a = [5, 12, 8, 130, 44]; let res = a[0]; a.forEach(e => { if (e < res) { res = e; } }); console.log(res); Output5 5. Using sort() Method (Not Efficient)This approach sorts the array in ascending order and picks the first element. It’s simple but not efficient for this task, as sorting has a time complexity of O(n log n), which is slower than simply iterating through the array. JavaScript const a = [5, 12, 8, 130, 44]; a.sort((x, y) => x - y); const res = a[0]; console.log(res); Output5 6. Using Math.min.apply() (Efficient for Any Array)This method is similar to using the spread operator but uses apply() to pass the array as arguments to Math.min(). It’s less readable but still efficient, with O(n) time complexity, and is useful in environments that don’t support the spread operator. JavaScript const a = [5, 12, 8, 130, 44]; const res = Math.min.apply(null, a); console.log(res); Output5 Comment More infoAdvertise with us Next Article JavaScript – Min Element in an Array M meetahaloyx4 Follow Improve Article Tags : JavaScript Web Technologies javascript-array Similar Reads 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 Find Second Smallest Element in an Array in JavaScript 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 a 2 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 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 Program to Find closest number in array Given an array of sorted integers. We need to find the closest value to the given number. Array may contain duplicate values and negative numbers. Examples: Input : arr[] = {1, 2, 4, 5, 6, 6, 8, 9} Target number = 11 Output : 9 9 is closest to 11 in given array Input :arr[] = {2, 5, 6, 7, 8, 8, 9}; 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 Javascript Program To Find Next Greater Element Given an array, print the Next Greater Element (NGE) for every element. The Next greater Element for an element x is the first greater element on the right side of x in the array. Elements for which no greater element exist, consider the next greater element as -1. Examples: For an array, the righ 6 min read Javascript Program for Minimum product subset of an array Given an array a, we have to find the minimum product possible with the subset of elements present in the array. The minimum product can be a single element also.Examples: Input : a[] = { -1, -1, -2, 4, 3 }Output : -24Explanation : Minimum product will be ( -2 * -1 * -1 * 4 * 3 ) = -24Input : a[] = 3 min read Javascript Program For Rearranging An Array In Maximum Minimum Form - Set 2 (O(1) extra space) Given a sorted array of positive integers, rearrange the array alternately i.e first element should be the maximum value, second minimum value, third-second max, fourth-second min and so on. Examples:Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: arr[] = {7, 1, 6, 2, 5, 3, 4}Input: arr[] = {1, 2, 3, 4 5 min read How to find min/max values without Math functions in JavaScript ? In this article, we will learn how to find the minimum and maximum values in an array without using Math functions. We know that Math.max() returns the maximum number passed in an array whereas Math.min() returns the minimum number passed. Approach: The same functionality of the Math functions can b 3 min read Like