最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get maxmin from array of strings (javascript) - Stack Overflow

programmeradmin0浏览0评论

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
  • 1 what is max and min in this case? – Nina Scholz Commented Feb 13, 2019 at 15:12
  • 2 What do you consider the 'min' and 'max' of a string in this case? – Igy Commented Feb 13, 2019 at 15:12
  • 1 Well if sorting would provide a meaningful answer then something like .localeCompare() could be used in a single pass. – Pointy Commented Feb 13, 2019 at 15:13
  • 3 The OP said he considered sort, so max / min one would assume is alpha sorted, first & last. – Keith Commented Feb 13, 2019 at 15:14
  • 1 Iterate through your list and update min and max in each iteration step – Thomas Sablik Commented Feb 13, 2019 at 15:14
 |  Show 5 more comments

6 Answers 6

Reset to default 9

To 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]],
  ),
);

发布评论

评论列表(0)

  1. 暂无评论