Skip to content
Advertisement

Get max / min from array of strings (javascript)

Any ideas to calculate min / max from array of strings?

JavaScript

So far i have considered to use .sort() function depending max / min, and get first element of result.

But maybe you know better preforming solution?

EDIT: Array size is below 1k elements. By Min /max i meant alphabetic sort: first and last element.

Advertisement

Answer

Iterate through your list and update min and max in each iteration step

JavaScript

It should be much faster than sort() for large arrays. This algorithm is O(n) while sort() is at least O(n log(n)).

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement