1

How to get random items from an array with no repeat?

I have an array of elements like

var a = ["Mango", "Orange", "Banana", "Apple", "Grapes", "Berry", "Peach"]

I want to get 3 random items from array a

var random = ["Banana", "Berry", "Peach"]
7

5 Answers 5

2

var a = ["Mango","Orange","Banana","Apple","Grapes","Berry","Peach"];

function searchRandom(count, arr){
  let answer = [], counter = 0;
 
  while(counter < count){
    let rand = arr[Math.floor(Math.random() * arr.length)];
    if(!answer.some(an => an === rand)){
      answer.push(rand);
      counter++;
    }
  }
  
  return answer;
}

console.log(searchRandom(3,a))

Making it flexible to support any count you want and ensured uniqueness

Sign up to request clarification or add additional context in comments.

Comments

1

var a = ["Mango","Orange","Banana","Apple","Grapes","Berry","Peach"]
var res = a.sort(function() {
  return 0.5 - Math.random();
});
console.log(res.slice(a,3))

1 Comment

Careful, .sort sorts the array in place, modifying the original one. You should make a copy and sort that one if you don't want any side effect.
0

This should work without any duplicates:

const a = ["Mango","Orange","Banana","Apple","Grapes","Berry","Peach"]
const b = a.slice()
const newArr = [];

for(let i= 0; i<3; i++){
 let arr = b[Math.floor(Math.random()*b.length)];
  
 let index = b.indexOf(arr);
  
  b.splice(index, 1 );
  
  newArr.push(arr)
  
}

console.log(newArr)

Comments

0

Try this:-

var a = ["Mango","Orange","Banana","Apple","Grapes","Berry","Peach"]
b = _.shuffle(a).slice(0,5);
console.log(b);

Comments

0

i would like to improve the Willem van der Veen's answer because his code might return undefined. It stems from the fact that the arrayClone takes out the random item but random number still in the range of the previous index length. Hence, the solution is we have to decrease the arrayClone's length too:

    const array = [0, 1 ,2 ,3 ,4, 5, 6, 7, 8 , 9]

    const arrayClone = array.slice()

    let arrayLength = array.length

    let newArray = []

    for (  let i = 0; i < 5; i++) {
        let arr = arrayClone[Math.floor(Math.random() * arrayLength)]

        arrayLength--

        let index = arrayClone.indexOf(arr)

        arrayClone.splice(index, 1)

        newArray.push(arr)
     }


      console.log(newArray)

1 Comment

Good answer, and we can improve little bit like this; const array = [0, 1 ,2 ,3 ,4, 5, 6, 7, 8 , 9] const arrayClone = array.slice(); let arrayLength = array.length; let newArray = []; for ( let i = 0; i < 5; i++) { var random = Math.floor(Math.random() * (arrayLength--)); newArray.push(arrayClone.splice(random, 1)[0]); //arrayClone.splice(random, 1) returns removed items, here we removed one item so we can simply get 0 element of removed item } console.log(newArray);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.