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

175
Visualizações
Random numbers in array with no sequential repeats

Does anyone know how to order numbers in array with no repeat.

I have situation to generate array of numbers like in sample below:

// some function to generate this array of numbers
function genNumbers() {
  // ...code to generate numbers and final return
  return [1,1,8,2,5,9,1,3,3,4] // return this array of numbers
}

Generated numbers is ok, but I need this numbers in non-repeat order like this:

[1,8,1,2,5,9,1,3,4,3] // all numbers from generated function but in different order.

Array contain more same number like 1 and 3 from sample but not one after the other.

Thank you!

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

0

One of the commenters suggested a good approach: pick randomly, just don't pick the last one you picked. At the outline level...

function arrayWithNoRepeat(length) {
  const result = [];
  for (let i=0; i<length; i++) {
    let lastValue = i ? result[i-1] : null;
    result.push(randomButNot(lastValue));
  }
  return result;
}

How to build randomButNot()? Here are a couple alternatives:

For a small range, build the set of pick-able values and pick one...

// the range of this function is ints 0-9 (except anInt)
function randomButNot(anInt) {
  const inBoundsValues = [0,1,2,4,5,6,7,8,9].filter(n => n!==anInt);
  const randomIndex = Math.floor(Math.random()*inBoundsValues.length);
  return inBoundsValues[randomIndex];
}

For a large range, one idea is to choose in a loop that guards against the duplicate...

// the range is 0-Number.MAX_SAFE_INTEGER (except anInt)
function randomButNot(anInt) {
  const choose = () => Math.floor(Math.random()*Number.MAX_SAFE_INTEGER)
  let choice = choose();
  // we'd have to be very unlucky for this loop to run even once
  while (choice === anInt) choice = choose();
  return choice;
}

There's a longer discussion about other alternatives relating range size, speed, determinism and uniformity of distribution, but I'll leave it to others smarter than me.

about 4 years ago · Juan Pablo Isaza Relatório

0

Try this,

function genNumbers() {
    // ...code to generate numbers and final return
    // let random = Array.from({ length: 10 }, () => Math.floor(Math.random() * 40)); //use this to generate random numbers
    let random = [5, 3, 6, 8, 1, 2, 2, 4, 9, 1]; // use this to generate fixed numbers
    let randomSorted = random.sort((a, b) => {
        return a - b;
    }) // sort the numbers
    let filterSame = randomSorted.filter((item, index) => {
        return randomSorted[index + 1] !== item;
    }) // filter the same numbers

    return filterSame // return the final result
}

console.log(genNumbers());

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Set object. Mdn

let myList = [1,8,1,2,5,9,1,3,4,3]

let unique = [... new Set(myList)]

unique.sort((a,b)=> {return a-b})

console.log(unique)

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