4

I'm trying to make an array of random colors from another array.

 String [] colors = new String[6];
         colors[0] = "red";
         colors[1] = "green";
         colors[2] = "blue";
         colors[3] = "yellow";
         colors[4] = "purple";
         colors[5] = "orange";

That's my array as of now. I want to make a new array with just 4 of those colors without duplicates.

So far I know how to make an array of randoms; however, I don't know how to take care of duplicates efficiently.

5
  • 2
    Add them to a LinkedList, and take (remove) from a random index between 0 and size() Commented Oct 3, 2013 at 20:32
  • 2
    You have few ways to achieve this. You can unsort a list of numbers from 0,1,2,3,4,5 and get the first 4 after the unsort. By unsort I mean shuffle them. You can randomly remove one of the elements. You can keep adding them to a Set collections, although it may take a while to finally add them all. Commented Oct 3, 2013 at 20:33
  • 1
    loop and add to a HashSet until the size of the HashSet is 4 would be by approach Commented Oct 3, 2013 at 20:33
  • I strongly suggest Knuth's shuffling algorithm, provides very good randomness for shuffling. You can pick the DataStructure of your choice to remove dups.. Commented Oct 3, 2013 at 20:37
  • This question gets asked about 5 times a week -- how to make a random selection without replacement. Please spend a little time searching for the answer. Commented Oct 3, 2013 at 20:37

4 Answers 4

2

Sounds like you want a Set. A Set is designed to remove duplicates.

Set<String> set = ...
for(String s : "a,b,c,d,e,f,d,e,c,a,b".split(","))
    set.add(s);

This set will have all the unique strings.

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

2 Comments

This is a completely non-Java way to program. Looks more like lisp/clojure. Get some sequences and go!
@SotiriosDelimanolis It uses methods from java.lang, does get more Java than that. ;)
2
List<String> colourList = new ArrayList<String>(Arrays.asList(colors));
Collections.shuffle(colourList);
return colourList.subList(0,4).toArray();

Comments

0

I'd strongly suggest you don't use arrays for this. Add what you need to a Set and it will handle duplicate management for you. You can always convert back to an array if need be.

Comments

0

You can just select random entries from colors, and add them into a Set until the set has four elements:

Set<String> randomStrings = new HashSet<String>();
Random random = new Random();
while( randomStrings.size() < 4) {
    int index = random.nextInt( colors.length);
    randomStrings.add( colors[index]);
}

You can try it out in this demo, where it selects four colors at random when you run it. You'll get output similar to:

Random colors: [orange, red, purple, blue]

2 Comments

But you have to do it without replacement.
This IS without replacement. It's going into a Set.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.