3

I want to merge 3 sets and then itereate over their union, but I'd like this union to be randomized.

If I do:

const a = new Set([1,2,3]);
const b = new Set([10,20,30]);
const c = new Set([100,200,300]);
const d = new Set([...a, ...b, ...c]);
const iterator = d.values();
for (let i = 0; i < 9; i++) {
    console.log(iterator.next());
}

I obviously get them in order, instead of having them shuffled. Is there another (more idiomatic and/or effifcient) way than using a shuffling array function as from this question like this:

let setArray = Array.from(d);
shuffleArray(setArray);
const randomSet = new Set(setArray);
ted
  • 13,596
  • 9
  • 65
  • 107
  • If you want to just shuffle the array then here? https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array – Chris Cousins Aug 31 '18 at 15:26
  • this is already referenced in my question. I'm not saying I don't have a way. I'm asking if there are better ways. – ted Aug 31 '18 at 15:28
  • Better how? Smaller? Faster? More easy to understand? – Chris Cousins Aug 31 '18 at 15:31
  • More idiomatic to Javascript and efficient. Having to do a return trip to ArrayLand seems like a workaround and I thought there would be other ways. – ted Aug 31 '18 at 15:34

3 Answers3

2

Randomize the arrays before you make the set, that way you get the set's benefit of removing duplicates but the ability for its data to be shuffled. This assumes a lot of things though (do you want to re-shuffle often?, will you be adding more items to the set later? ...)

const arr1 = [1, 2, 3];
const arr2 = [10, 20, 30];
const arr3 = [100, 200, 300];
const arrs = shuffle([ ...arr1, ...arr2, ...arr3 ]);

const d = new Set(arrs);
const iterator = d.values();
for (let i = 0; i < 9; i++) {
    console.log(iterator.next());
}
Chris Cousins
  • 1,862
  • 8
  • 15
  • duplication-wise, it seems to me that merging sets before shuffling arrays is more efficient as there would be fewer elements to shuffle. But thanks for the idea anyways! – ted Aug 31 '18 at 16:04
  • Yes it depends on the number of elements, whether later you prefer to keep things in sets or as arrays. Interesting stuff. – Chris Cousins Aug 31 '18 at 16:18
0

if you asking is there a better function then maybe How can I shuffle an array? (ES2015 (ES6)) this will help.

if i dont understand your question then sorry.

  • no the question is not about arrays, it's about sets, and how to shuffle them without having to use arrays, otherwise, as you can see, I already know how to do so – ted Aug 31 '18 at 16:05
  • oh... sorry then. –  Aug 31 '18 at 16:29
0

Here's an answer that might not be fancy, but works. In Pseudo-code:

  1. Combine your arrays (which is looks like you're doing) either using spread or concat
  2. Create a new array which will hold your randomized shuffle. For now, it's empty.
  3. While your new array isn't the length of your merged array, pick a random number from the merged array. If that number isn't included in your new array, add it to your new array.