Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifying duplicates before adding them into an array

I am attempting 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 must check that the number is not a duplicate and not already in the array.

I seem to be fairly close to the correct output, however for some reason, i repeatedly get the number 0 as the first element in my array, and it is also the only number that is ever duplicated. Does anyone know why this is, and if so be able to provide a suitable fix?

Once a duplicate is found, it needs to be printed to the output, and replaced by 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));
}

}

like image 622
freshwaterjoe Avatar asked Jun 12 '26 08:06

freshwaterjoe


1 Answers

j is always greater than i, because you initialize j as i+1. That means that the values of r50 that are referenced by j are always 0, so those will always be the duplicates.

For example, if i = 20, in the second loop, j will start at 21. r50[21], r50[22], etc... are all 0, because you haven't set them yet, so the only possible duplicate of r50[i] and r50[j] is 0.

Edit: If the point of j is to iterate through all the previous elements of the array, then you'll 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);
            }
    }
}

Though this still won't work perfectly, because you might set r50 to an earlier value, after you checked it. For example, if you made sure that r50[20] isn't equal to any values of j through 10, and then it is equal to r50[11] (when j = 11), then you might accidentally change it back to a value of j less than that (for example, r50[5]).

I think the neatest way is, as Duncan and Rajeev have,

HashSet numbers = new HashSet();
Random rand = new Random();

while(numbers.size() < MAX_SIZE) {
    numbers.add(rand.nextInt(1000));
}
like image 133
Sherz Avatar answered Jun 13 '26 21:06

Sherz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!