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

305
Visualizações
How do you find the 10 indexes of a javascript array that have the highest numeric values?

I am counting the frequency of numbers between 0 and 256 found in a sequence using a javascript array.

If I see the number 74 pop up I am storing it in the 74th index value (i.e. mysequence[74] = mysequence[74] + 1)

I can see the results tallied and its easy when scanning it visually to see that certain numbers (for example 65) has appeared much more often than others. I want to find the 10 numbers that appear most often.

I am concerned a simple sort will not retain the index value which is what I am using to track which number is associated with the counted frequency.

The only thought that comes to mind would be a brute force approach. Creating a function to look through all values one by one keeping the highest and ignoring all indexes that are part of an ignore list. Then running that function 10 times, each subsequent time passing the values of the indexes that have been assigned the top values.

Is there a way to get the numeric keys associated with the top 10 values without resorting to the approach above? (i.e. maybe converting my data to a different format and some fancy map sorting functions?)

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

0

You could get the indices and sort them by using the values and get the top wanted indices.

const
    values = [7, 3, 4, 5, 2, 8],
    indices = [...values.keys()].sort((a, b) => values[b] - values[a]);

console.log(indices.slice(0, 3));

about 4 years ago · Juan Pablo Isaza Relatório

0

If you just sort it right away, you are definitely going to lose information about the indexes. What you can do is map the array with the values to an array with the index and the value (so that you don't lose information later), then you sort it, but you change the criteria of the sort method in order to get the indexes with top values.

const arr = [2, 1, 0, 3];

const getTopN = (arr, n = 10) => {
  const _arr = arr.map((value, index) => [value, index]);
  // by using b[0] - a[0] instead of a[0] - b[0] we can get the array in non-increasing order
  _arr.sort((a, b) => b[0] - a[0]) 
  return _arr.slice(0, n).map(([_, index]) => index);
}

console.log(getTopN(arr))

The getTopN function does the job.

about 4 years ago · Juan Pablo Isaza Relatório

0

There might be other ways, but what comes to me off the top of my head is to map the original array to a set of value/index pairs, sort the result based on value, then grab the last (or first) 10 items of the sorted array (depending on whether or not you did an ascending or descending sort).

yourArray.map((value, index) => ({ value, index }))
  .sort((a, b) => b.value - a.value)
  .slice(0, 10)
  .map(obj => obj.index);

You can omit the last map if you want to keep both the indices and the values that go with them in your result.

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