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

122
Visualizações
How to randomize an array, leaving the items that are the same far from each other?

Basically I have an array where there are similar items close by, but I would like to find a way where I leave these items that are the same apart from each other ex:

var array = [1,2,3,3,4]; 

var myWishArray = [1,2,3,4,3];
||
var myWishArray = [3,2,3,4,1];
||
var myWishArray = [3,2,3,1,4];
...

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

0

If it's enough that two equal numbers are never neighbors, then you can

  • sort the list
  • fill a new array with these numbers in two passes, leaving a space of one

a = [1, 1, 1, 1, 2, 3, 2, 3, 4, 4, 4];
a.sort();
b = Array(a.length);
for (i = 0; i < a.length; i ++) {
    if (i * 2 < a.length) {
        b[i * 2] = a[i];
    } else {
        start = i - Math.ceil(a.length / 2)
        b[start * 2 + 1] = a[i];
    }
}
console.log(b);

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