How to find min element in array javascript?
How about augmenting the built-in Array object to use
Show
Here is a JSFiddle. Augmenting the built-ins can cause collisions with other libraries (some see), so you may be more comfortable with just
Alternately, assuming your browser supports ECMAScript 6, you can use spread syntax which functions similarly to the
RobG 137k30 gold badges167 silver badges205 bronze badges answered Nov 3, 2009 at 18:23
Roatin MarthRoatin Marth 22.8k3 gold badges51 silver badges55 bronze badges 14 For big arrays (~10⁷ elements),
A more robust solution is to not add every element to the call stack, but to instead pass an array:
If you are concerned about speed, the following code is ~3 times faster then
If your arrays contains strings instead of numbers, you also need to coerce them into numbers. The below code does that, but it slows the code down ~10 times on my machine. See https://jsben.ch/uPipD.
answered Nov 18, 2012 at 14:00
Linus UnnebäckLinus Unnebäck 20.9k12 gold badges70 silver badges85 bronze badges 6 Using spread operator (ES6)
Gass 4,8172 gold badges17 silver badges29 bronze badges answered Aug 23, 2016 at 16:37
Abdennour TOUMIAbdennour TOUMI 79.5k36 gold badges233 silver badges239 bronze badges 10 tl;dr
MDN solutionThe official MDN docs on
Maximum size of an arrayAccording to MDN the
They even provide a hybrid solution which doesn't really have good performance compared to other solutions. See performance test below for more. In 2019 the actual limit is the maximum size of the call stack. For modern Chromium based desktop browsers this means that when it comes to finding min/max with
With the script below (based on this blog post), by catching that error you can calculate the limit for your specific environment. Warning! Running this script takes time and depending on the performance of your system it might slow or crash your browser/system!
Performance on large arraysBased on the test in EscapeNetscape's comment I created some benchmarks that tests 5 different methods on a random number only array with 100000 items. In 2019, the results show that the standard loop (which BTW doesn't have the size limitation) is the fastest
everywhere. Almost all tests gave the same results, except for one where spread somewhy ended up being the slowest. If you step up your array to have 1 million items, things start to break and you are left with the standard loop as a fast solution and JSPerf benchmarkJSBen benchmarkJSBench.me benchmarkBenchmark source codeanswered Jun 14, 2015 at 21:22
totymedlitotymedli 27.2k20 gold badges127 silver badges158 bronze badges 4 If you're paranoid like me about using
Or, in ES6:
The anonymous functions are unfortunately necessary (instead of using answered Jul 27, 2015 at 1:00
7 Alternative MethodsThe Using them with an array that contains large number of items (more than ~10⁷ items, depends on the user's browser) most likely will crash and give the following error message:
UPDATE Instead, consider using something like so:
Or with better run-time:
Or to get both Min and Max:
Or with even better run-time*:
* Tested with 1,000,000 items: answered Oct 2, 2018 at 17:34
Lior ElromLior Elrom 18.3k16 gold badges76 silver badges90 bronze badges 3 Two ways are shorter and easy:
Way 1:
Way 2:
answered May 18, 2018 at 1:37
3
The
The
However, as of the ECMAScript 6 you can use the spread operator:
Using the spread operator, the above can be rewritten as such:
When calling a function using the variadic operator, you can even add additional values, e.g.
Bonus: Spread operator enables you to use the array literal syntax to create new arrays in situations where in ES5 you would need to fall back to imperative code, using a combination of
answered Dec 18, 2014 at 1:38
GajusGajus 64.5k68 gold badges262 silver badges417 bronze badges 1 You do it by extending the Array type:
Boosted from here (by John Resig) answered Nov 3, 2009 at 18:35
brettkellybrettkelly 27k8 gold badges53 silver badges70 bronze badges A simple solution to find the minimum value over an
or using JavaScript's built-in Math.Min() function (thanks @Tenflex):
This sets EDIT: Include position of minimum value:
answered Oct 29, 2017 at 11:26
2 For a concise, modern solution, one can perform a
To only find either the minimum or maximum, we can use perform a reduce operation in much the same way, but we only need to keep track of the previous optimal value. This method is better than using
answered Aug 20, 2020 at 22:47
UnmitigatedUnmitigated 48.7k7 gold badges49 silver badges64 bronze badges Others have already given some solutions in which they augment When passing
The above will throw an exception because You can pretty much assume that nobody has decorated answered Nov 3, 2009 at 18:39
Ionuț G. StanIonuț G. Stan 171k18 gold badges187 silver badges199 bronze badges 2 One more way to do it:
Usage:
gion_13 40.7k10 gold badges96 silver badges107 bronze badges answered Sep 26, 2012 at 18:43
sbrsbr 4,5495 gold badges41 silver badges46 bronze badges 2 I am surprised not one mentiond the reduce function.
2 This may suit your purposes.
answered Nov 3, 2009 at 18:21
ChaosPandionChaosPandion 76.1k17 gold badges117 silver badges154 bronze badges 5 let array = [267, 306, 108] let longest = Math.max(...array); answered Oct 3, 2020 at 15:10
Trilok SinghTrilok Singh 1,05911 silver badges8 bronze badges 1 I thought I'd share my simple and easy to understand solution. For the min:
And for the max:
answered Oct 13, 2016 at 16:37
IonutIonut 10.7k4 gold badges43 silver badges69 bronze badges 9 For
big arrays (~10⁷ elements), For big arrays, a quick & dirty solution is:
answered Jan 24, 2012 at 12:43
PeterPeter 5,0485 gold badges27 silver badges38 bronze badges Aside using the math function max and min, another function to use is the built in function of sort(): here we go
answered Feb 14, 2020 at 15:29
Chukwu3mekaChukwu3meka 4,2954 gold badges28 silver badges52 bronze badges 1 I had the same problem, I needed to obtain the minimum and maximum values of an array and, to my surprise, there were no built-in functions for arrays. After reading a lot, I decided to test the "top 3" solutions myself:
The test code was this:
The array A was filled with 100,000 random integer numbers, each function was executed 10,000 times on Mozilla Firefox 28.0 on an intel Pentium 4 2.99GHz desktop with Windows Vista. The times are in seconds, retrieved by performance.now() function. The results were these, with 3 fractional digits and standard deviation:
The REDUCE solution was 117% slower than the discrete solution. The APPLY solution was the worse, 2,118% slower than the discrete solution. Besides, as Peter observed, it doesn't work for large arrays (about more than 1,000,000 elements). Also, to complete the tests, I tested this extended discrete code:
The timing: mean=0.218s, sd=0.094 So, it is 35% slower than the simple discrete solution, but it retrieves both the maximum and the minimum values at once (any other solution would take at least twice that to retrieve them). Once the OP needed both values, the discrete solution would be the best choice (even as two separate functions, one for calculating maximum and another for calculating minimum, they would outperform the second best, the REDUCE solution). answered Apr 2, 2014 at 17:46
CyberknightCyberknight 1582 silver badges6 bronze badges Iterate through, keeping track as you go.
This will leave min/max null if there are no elements in the array. Will set min and max in one pass if the array has any elements. You could also extend Array with a
Used as
answered Nov 3, 2009 at 18:23
tvanfossontvanfosson 513k97 gold badges694 silver badges793 bronze badges 1 You can use the following function anywhere in your project:
And then you can call the functions passing the array:
falsarella 12.1k9 gold badges69 silver badges113 bronze badges answered Aug 26, 2014 at 16:57
The following code works for me :
Gogol 2,9834 gold badges27 silver badges54 bronze badges answered May 26, 2017 at 12:40
0 For an array containing objects instead of numbers:
You can use
or the largest value (max)
answered Aug 6, 2020 at 12:17
laktaklaktak 53.9k16 gold badges128 silver badges159 bronze badges
answered Oct 28, 2020 at 21:53
Gives you the maximum value in an array of numbers.
Gives you the minimum value in an array of numbers.
answered Jun 20, 2020 at 20:33
Adam BelekoAdam Beleko 4555 silver badges9 bronze badges Simple stuff, really.
falsarella 12.1k9 gold badges69 silver badges113 bronze badges answered Sep 23, 2014 at 7:48
BrianBrian 3,6231 gold badge21 silver badges33 bronze badges Here's one way to get the max value from an array of objects. Create a copy (with slice), then sort the copy in descending order and grab the first item.
falsarella 12.1k9 gold badges69 silver badges113 bronze badges answered Jan 9, 2014 at 18:46
BenBen 5679 silver badges8 bronze badges How do you find the minimum element of an array?ALGORITHM:. STEP 1: START.. STEP 2: INITIALIZE arr[] = {25, 11, 7, 75, 56}. STEP 3: length= sizeof(arr)/sizeof(arr[0]). STEP 4: min = arr[0]. STEP 5: SET i=0. REPEAT STEP 6 and STEP 7 UNTIL i
STEP 6: if(arr[i]
STEP 7: i=i+1.. STEP 8: PRINT "Smallest element present in given array:" by assigning min.. How do you find min and max value in an array in JavaScript?Array.prototype.reduce() can be used to find the maximum element in a numeric array, by comparing each value:. const arr = [1, 2, 3]; const max = arr. reduce((a, b) => Math. max(a, b), -Infinity); ... . function getMaxOfArray(numArray) { return Math. max. ... . const arr = [1, 2, 3]; const max = Math. max(.... What does Arr MIN () do?Maximum and minimum of an array using minimum number of comparisons. Given an array of size N. The task is to find the maximum and the minimum element of the array using the minimum number of comparisons.
What is the minimum value in JavaScript?JavaScript Number MIN_VALUE
MIN_VALUE returns the smallest number possible in JavaScript. Number. MIN_VALUE has a value of 5e-324.
|