how to create an array with unique values

Solutions on MaxInterview for how to create an array with unique values by the best coders in the world

showing results for - "how to create an array with unique values"
Deana
07 Nov 2018
1import java.util.Random;
2
3public class GenRandArray {
4    //Recommend reseeding within methods
5    static Random rand = new Random();
6
7    public static void fillInt( int[] fillArray ) {
8        //seeds the random for a new random each method call
9        rand.setSeed(System.currentTimeMillis());
10		
11        for(int i = 0; i < fillArray.length; i++)
12        {
13            fillArray[i]= rand.nextInt();
14            
15          	/* for loop to check to make sure that none of the previous
16               numbers added are the same as the one just added. */
17            for (int j = 0; j < i; j++)
18            {
19              	/* if so we need to redo the last random number by
20              	   moving i back one index to be re-set */
21                if (fillArray[i] == fillArray[j])
22                {
23                    i--;
24                }
25            }
26        }
27    }
28}
similar questions
unique numbers in array