Basic
Basic

Reputation: 1

hashset to remove duplicates from array Java

I'm currently trying to get my java program to not print out duplicate laws in print out. The software is random picking to print out 1 to 3 laws but I don't want duplicates. I'm still kind of new to java and only have written a few programs so far. This is the most complicated one I've written so far.

Right now I want the software to run the random again if it finds a duplicate till there are no duplicates in the print. Is there anyway to do do this ?

/**
    *Seeds is a random number to pick which if statement will be used. 
    * telling the software how many laws to use during its random 
    * generation process
    * 
    */
    Random seeds = new Random(); 
    seed = seeds.nextInt(3) + 1; 

    if (seed == 1){


        /**Beginning of random law selection for universe. Must find way 
         *Must find way to get rid of duplicate answers 
         */
        final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
            "Psionics", "Substandard Physics","Exotic"};
            Random random = new Random();
            int index = random.nextInt(Laws.length);
            System.out.println("Law:" + Laws[index]);
    }

    else if (seed == 2) {

        final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
            "Psionics", "Substandard Physics","Exotic"};
            Random random = new Random();
            int index = random.nextInt(Laws.length);
            int index2 = random.nextInt(Laws.length);
            System.out.println("Law:" + Laws[index] + "," + Laws[index2]);

    }

    else {

        final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
            "Psionics", "Substandard Physics","Exotic"};
            Random random = new Random();
            int index = random.nextInt(Laws.length);
            int index2 = random.nextInt(Laws.length);
            int index3 = random.nextInt(Laws.length);
            System.out.println("Law: " + Laws[index] + "," + Laws[index2] + 
                    "," + Laws[index3]);

    }

Upvotes: 0

Views: 225

Answers (2)

Huzaima Khan
Huzaima Khan

Reputation: 63

Here is one approach. You've 3 if conditions so take 3 boolean variables and associate one with each if and make it true when that if is executed.

Random seeds = new Random(); 
    seed = seeds.nextInt(3) + 1; 


boolean selected[] = new boolean[3];
selected[0] = selected[1] = selected[2] = false;//initially none of the if is executed

    if (seed == 1 && !selected[seed-1]){//checking if not previously selected and "seed-1" since array indexes are from 0 to n-1


        /**Beginning of random law selection for universe. Must find way 
         *Must find way to get rid of duplicate answers 
         */
        final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
            "Psionics", "Substandard Physics","Exotic"};
            Random random = new Random();
            int index = random.nextInt(Laws.length);
            System.out.println("Law:" + Laws[index]);
            selected[seed-1] = true;
    }

    else if (seed == 2 && !selected[seed-1]) {

        final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
            "Psionics", "Substandard Physics","Exotic"};
            Random random = new Random();
            int index = random.nextInt(Laws.length);
            int index2 = random.nextInt(Laws.length);
            System.out.println("Law:" + Laws[index] + "," + Laws[index2]);
            selected[seed-1] = true;
    }

    else if (!selected[seed-1]){

        final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
            "Psionics", "Substandard Physics","Exotic"};
            Random random = new Random();
            int index = random.nextInt(Laws.length);
            int index2 = random.nextInt(Laws.length);
            int index3 = random.nextInt(Laws.length);
            System.out.println("Law: " + Laws[index] + "," + Laws[index2] + 
                    "," + Laws[index3]);
        selected[seed-1] = true;
    }

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533500

Another way to think of this problem is; you want to select values in a random order. This way you won't get duplicates unless an element appears more than once.

List<String> laws = Arrays.asList("Standard Physics", "Magic", "Mad Science", 
        "Psionics", "Substandard Physics","Exotic");
Collections.shuffle(laws);
// select as many as you need
List<String> lawsSelected = laws.subList(0, 3);

Upvotes: 2

Related Questions