Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

180
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda