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 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 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 Collect.js min() Method The min() method is used to return the minimum element from the given array or collection. The method can be specified with a key as a parameter so that only the values of that key in the collection would be considered and the minimum element would be found from those values. Syntax: collect(array). 1 min read Javascript Program for Find the smallest missing number Given a sorted array of n distinct integers where each integer is in the range from 0 to m-1 and m > n. Find the smallest number that is missing from the array. Examples:Input: {0, 1, 2, 6, 9}, n = 5, m = 10 Output: 3 Input: {4, 5, 10, 11}, n = 4, m = 12 Output: 0 Input: {0, 1, 2, 3}, n = 4, m = 4 min read JavaScript Program for the Minimum Index of a Repeating Element in an Array Given an array of integers arr[], The task is to find the index of the first repeating element in it i.e. the element that occurs more than once and whose index of the first occurrence is the smallest. In this article, we will find the minimum index of a repeating element in an array in JavaScript.E 5 min read JavaScript Program to Find Maximum of Minimum for Every Window Size in a Given Array Given an array of integers, find the maximum of minimums for every window size in the array. The window size varies from 1 to the size of the array. Example: Input: Array: [1, 2, 3, 5, 1, 7, 3]Output: [7, 3, 2, 1, 1, 1, 1]Explanation: The first element in the output indicates the maximum of minimums 4 min read Java Program to Print the Smallest Element in an Array Java provides a data structure, the array, which stores the collection of data of the same type. It is a fixed-size sequential collection of elements of the same type. Example: arr1[] = {2 , -1 , 9 , 10} output : -1 arr2[] = {0, -10, -13, 5} output : -13 We need to find and print the smallest value 3 min read Java | Collectors minBy(Comparator comparator) with Examples Collectors minBy(Comparator<? super T> comparator) is used to find an element according to the comparator passed as the parameter. It returns a Collector that produces the minimal element according to a given Comparator, described as an Optional<T>. Syntax: public static <T> Collec 4 min read Like