dimanche 28 février 2016

How to remove duplicates from array using for loop

In this code I have found duplicates from an array and I want to remove them. The output then will be unique generated numbers. I am required to use math.random and modulo. Anyone have any clues?I tried to store them in an array but then the original array has 0's and 0 is part of my domain for the random number generation (from 0 to 52).

public class Decks {

 public static void main(String[] args) {

generate();

}

 public static void generate() {
int deckOfCard[] = new int[52];

for (int counts = 0; counts < 52; counts++) {

    deckOfCard[counts] = (int) (Math.random() * 51);

}

for (int i = 0; i < deckOfCard.length - 1; i++) {

    for (int j = i + 1; j < deckOfCard.length; j++) {

        if ((deckOfCard[i] == (deckOfCard[j])) && (i != j)) {

            System.out.println("DUPLICATE " + deckOfCard[i]);

        }
    }
}

for (int count = 0; count < deckOfCard.length; count++) {

    System.out.print("\t" + deckOfCard[count]);

}
}




Aucun commentaire:

Enregistrer un commentaire