'How to find smallest and biggest value from list of elements, in most efficient way?

I have a two sets of elements, one holds list of numbers and second of names. something like this.

A: 4,
B: 3,
C: 2,
A: 5,
C: 3,

And my task is to find elements with smallest value and highest value. I know that i could create array of objects and sort it with map [{A: 4},{C:2}....] But i was wondering is there any efficient ways of doing it. Instead of creating a have object, and use three loops. Would it be possible to replace it with something more efficient. Like set or something where i could just call set.getItemWithMinValue, set.getItemWithMaxValue and return would be : C:2, A:5

Sorry for silly quesition, i am still learning.



Solution 1:[1]

You are going to have to loop, parse the object into its values, and check if the value is greater or less.

var data = [
  { A: 4 },
  { B: 3 },
  { C: 2 },
  { A: 5 },
  { C: 3 },
];

const results = data.reduce((minMax, item) => {
  const value = Object.values(item)[0];
  if (!minMax) {
    minMax = {
      min: { value, item },
      max: { value, item },
    }
  } else if (minMax.min.value > value) {
    minMax.min = { value, item };
  } else if (minMax.max.value < value) {
    minMax.max = { value, item };
  }
  return minMax;
}, null); 

console.log(results.min.item);
console.log(results.max.item);

Solution 2:[2]

This would be one way of doing it. Caution: the array will be changed (sorted) in the course of the script.

const arr=[{A: 4},{B: 3},{C: 2},{A: 5},{C: 3}],
      val=o=>Object.values(o)[0];

arr.sort((a,b)=>val(a)-val(b));

console.log(arr[0],arr[arr.length-1])

Solution 3:[3]

if you have this data

const arr = [{A:2},{A: 4},{B: 3},{C: 2},{A: 5},{C: 3}];

you can iterate over it even if you don't know the property of the objects like this.รง

const arr = [{A:2},{A: 4},{B: 3},{C: 2},{A: 5},{C: 3}];
const result = arr.sort((prev, next) =>  {  
     const prevProp = Object.getOwnPropertyNames(prev); 
     const nextProp = Object.getOwnPropertyNames(next); 
     return prev[prevProp] - next[nextProp]
});
console.log('max',result[0]);
console.log('min',result[result.length - 1]);

Solution 4:[4]

You could take a single loop with getting the entries from the object.

This approach expects only a singl min and max value.

const
    array = [{ A: 4 }, { B: 3 }, { C: 2 }, { A: 5 }, { C: 3 }];
    
let min, max;

for (const object of array) {
    const [[k, v]] = Object.entries(object);
    if (!min || min[1] > v) min = [k, v];
    if (!max || max[1] < v) max = [k, v];
}

console.log('min', Object.fromEntries([min]));
console.log('max', Object.fromEntries([max]));

This approach respects more than one name with same min or max value.

const
    array = [{ A: 4 }, { B: 3 }, { C: 2 }, { A: 5 }, { C: 3 }, { F: 2 }];
    
let min, max;

for (const object of array) {
    const v = Object.values(object)[0];

    if (!min || min[1] > v) min = [[object], v];
    else if (min[1] === v) min[0].push(object);

    if (!max || max[1] < v) max = [[object], v];
    else if (max[1] === v) max[0].push(object);
}

console.log('min', min[0]);
console.log('max', max[0]);

Solution 5:[5]

This is probably premature optimisation but I'm going to leave it here in case it's useful to anyone. When you need both the minimum and the maximum, you can save one comparison for every two objects (that is, cut it down from two comparisons per object to three comparison per two objects) by taking the objects in pairs, comparing the pair of objects with each other (one comparison) and then comparing only the larger one with the accumulated maximum and only the smaller one with the accumulated minimum.

To start the procedure, you have the options of initialising both the maximum and the minimum with the first element, or of initialising the maximum as the larger of the first two elements and the minimum as the smaller of the two. If you know in advance how many elements there are, you can choose one or the other of these depending on whether the number of objects to scan is odd or even, so that the scan will always be over complete pairs.

The code is slightly more complicated, so it's only worth doing if the performance benefit is actually significant in your application. Or, I suppose, as a learning exercise.

Solution 6:[6]

You can use Array.prototype.reduce():

const array = [{ A: 4 }, { B: 3 }, { C: 2 }, { A: 5 }, { C: 3 }]
const result = array.reduce((a, c) => {
  const [v, max, min] = [c, a.max, a.min].map((o) => Object.values(o)[0])
  a.max = max > v ? a.max : c
  a.min = min < v ? a.min : c
  return a
},
{ max: {}, min: {} })

console.log(result)

Or you can use Array.prototype.sort():

const array = [{ A: 4 }, { B: 3 }, { C: 2 }, { A: 5 }, { C: 3 }]
const arraySorted = array.sort((a, b) => {
  const [aValue, bValue] = [a, b].map((o) => Object.values(o)[0])
  return bValue - aValue
})
const result = {
  max: arraySorted[0],
  min: arraySorted[arraySorted.length - 1],
}

console.log(result)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 epascarello
Solution 2
Solution 3
Solution 4
Solution 5
Solution 6