Any ideas to calculate min / max from array of strings?
var arr = ['aab','aac','aad','abx'];
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.
Any ideas to calculate min / max from array of strings?
var arr = ['aab','aac','aad','abx'];
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.
Share Improve this question edited Feb 13, 2019 at 15:27 Federico klez Culloca 27.1k17 gold badges59 silver badges100 bronze badges asked Feb 13, 2019 at 15:10 IT ManIT Man 1,0364 gold badges15 silver badges35 bronze badges 10 | Show 5 more comments6 Answers
Reset to default 9To extend @Keith's answers.. if we want min
or max
only then reduce
will be 1-liner.
const arr = ['aab', 'aac', 'aad', 'abx']
const min = arr.reduce((min, c) => c < min ? c : min) // 'aab'
const max = arr.reduce((max, c) => c > max ? c : max) // 'abx'
Iterate through your list and update min and max in each iteration step
function getMinMax(arr) {
if (!arr) {
return null;
}
var minV = arr[0];
var maxV = arr[0];
for (a of arr) {
if (a < minV) minV = a;
if (a > maxV) maxV = a;
}
return [minV, maxV];
}
console.log(getMinMax(['abc', 'aaa', 'abb']));
It should be much faster than sort()
for large arrays. This algorithm is O(n) while sort()
is at least O(n log(n)).
If you don't want to use a sort,.
Another option is to use Array.reduce
to maintain the min
& max
values.
Below is a working snippet showing this.
ps. Your test data already had the min as the first element, and max as the last element, so I've altered the example array to have zzz
, that of course would be max
.
var arr = ['aab','aac','zzz','aad','abx'];
var ret = arr.reduce((a, v) => {
a.min = a.min === null ? v :
v.localeCompare(a.min) < 0 ? v : a.min;
a.max = a.max === null ? v :
v.localeCompare(a.max) > 0 ? v : a.max;
return a; },
{min: null, max: null});
console.log(ret);
This is what I did (note that it mutates the original array):
const myArray = ['beta', 'alpha', 'zeta', 'delta'];
const min = myArray.sort()[0];
const max = myArray.reverse()[0];
console.log({ min, max });
Here are a few solutions to wrap your head around:
Min/max based on length
const output = arr.sort((a, b) => a.length - b.length);
Min/max as in alphabetical order
const output = arr.sort();
Extract the minimum and max
const max = arr.sort(() => 1)[0];
You can use reduce in one pass but have to check what to return if the array is empty (currently returns undefined for both min and max)
const arr = [
'aab',
'aac',
'aad',
'abx',
];
console.log(
arr.reduce(
([min, max], item) => [
min.localeCompare(item) > 0
? item
: min,
max.localeCompare(item) < 0
? item
: max,
],
[arr[0], arr[0]],
),
);
max
andmin
in this case? – Nina Scholz Commented Feb 13, 2019 at 15:12.localeCompare()
could be used in a single pass. – Pointy Commented Feb 13, 2019 at 15:13max / min
one would assume is alpha sorted, first & last. – Keith Commented Feb 13, 2019 at 15:14