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!
CodePudding user response:
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());
CodePudding user response:
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)
CodePudding user response:
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.