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);
4
  • If you want to just shuffle the array then here? stackoverflow.com/questions/2450954/… Commented Aug 31, 2018 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. Commented Aug 31, 2018 at 15:28
  • Better how? Smaller? Faster? More easy to understand? Commented Aug 31, 2018 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. Commented Aug 31, 2018 at 15:34

3 Answers 3

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());
}
Sign up to request clarification or add additional context in comments.

2 Comments

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!
Yes it depends on the number of elements, whether later you prefer to keep things in sets or as arrays. Interesting stuff.
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.

2 Comments

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
oh... sorry then.
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.

Comments

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.