java - How to remove duplicates from array using for loop -
in code have found duplicates array , want remove them. output unique generated numbers. required use math.random , modulo. have clues? tried store them in array original array has 0's , 0 part of domain random number generation (from 0 52).
public class decks { public static void main(string[] args) { generate(); } public static void generate() { int deckofcard[] = new int[52]; (int counts = 0; counts < 52; counts++) { deckofcard[counts] = (int) (math.random() * 51); } (int = 0; < deckofcard.length - 1; i++) { (int j = + 1; j < deckofcard.length; j++) { if ((deckofcard[i] == (deckofcard[j])) && (i != j)) { system.out.println("duplicate " + deckofcard[i]); } } } (int count = 0; count < deckofcard.length; count++) { system.out.print("\t" + deckofcard[count]); } }
you must validate numbers generated during random number generation this:
import java.util.random; public class decks { public static void main(string[] args) { random myrandom = new random(); int[] num = new int[53]; boolean[] check = new boolean[53]; int = 0; int rannum; while (all < 53) { rannum = myrandom.nextint(53); if (!check[rannum]) { check[rannum] = true; num[all] = rannum; all++; } } (int = 0; < 53; i++) { system.out.println(num[i]); } } }
i suggest not including number 0
because not exist in real deck of cards (ace being lowest having number value of 1). included right here because in understanding, 0
included in desired output.
Comments
Post a Comment