Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

248
Visualizações
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.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

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])

about 4 years ago · Juan Pablo Isaza Relatório

0

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);

about 4 years ago · Juan Pablo Isaza Relatório

0

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda