Identifying duplicates before adding them to an array
I am trying to write a program that consists of an array filled with 50 random numbers between the values 1-999. However, before a random number is added to the array, I have to check that the number is not a duplicate and is not yet in the array.
I seem to be pretty close to the correct conclusion, however for some reason I am repeatedly getting the number 0 as the first element in my array and this is also the only number ever duplicated. Does anyone know why this is the case, and if so, can you provide a suitable fix?
Once a duplicate is found, it needs to be printed out and replaced with a new unique random number.
Thanks in advance.
import java.util.*;
public class Random50 {
public static void main (String[] args)
{
final int MAX_SIZE = 50;
int[] r50 = new int[MAX_SIZE];
boolean duplicates = false;
Random rand = new Random();
for (int i=0; i<r50.length; i++)
{
for (int j=i+1;j<r50.length;j++)
{
r50[i] = rand.nextInt(1000);
if (j!=i && r50[i] == r50[j])
{
duplicates = true;
System.out.println("DUPE: " + r50[i]);
r50[i] = rand.nextInt(1000);
}
}
}
System.out.println(Arrays.toString(r50));
}
}
source to share
j is always greater than i, because you initialize j as i + 1. This means that the r50 values referenced by j are always 0, so they will always be duplicates.
For example, if i = 20, in the second loop j will start at 21. r50 [21], r50 [22], etc. - all 0s because you haven't installed them yet, so the only possible duplicates of r50 [i] and r50 [j] are 0.
Edit: if point j is for repeating all previous elements of the array, then you will want
for (int i=0; i<r50.length; i++)
{
r50[i] = rand.nextInt(1000); //Set it before the j loop
for (int j = 0; j < i; j++)
{
while (r50[i] == r50[j]) //while loop, in case of multiple duplicates
{
duplicates = true; //Still not sure why you want this boolean
System.out.println("DUPE: " + r50[i]);
r50[i] = rand.nextInt(1000);
}
}
}
It won't work that way though, because you can set r50 to an earlier value after you've checked it. For example, if you made sure that r50 [20] is not equal to any values from j through 10, and then it is equal to r50 [11] (for j = 11), you might accidentally change it to a value of j less than that (for example, r50 [5]).
I think the easiest way is like Duncan and Rajiv,
HashSet numbers = new HashSet();
Random rand = new Random();
while(numbers.size() < MAX_SIZE) {
numbers.add(rand.nextInt(1000));
}
source to share
Performance wise, it's not as good as every time you compare a value with the following array positions. You have to use a hashing algorithm whereby you can know at what point an object might lie depending on its unique hashcode and an image appears here HashSet
that has O (1) for most operations, and this is much less likely to collide hash codes in Integer
class Objects
public static void main (String[] args)
{
final int MAX_SIZE = 50;
HashSet<Integer> hs = new HashSet<Integer>(50);
while(hs.size()<50){
Random rand = new Random();
int randomVal = rand.nextInt(1000);
hs.add(randomVal);
}
Integer[] r50 = hs.toArray();
}
source to share