0

I need a solution to create an array with random values and with no duplicates. My issue is when I create an array with no duplicates it removes the duplicates and keeps my array with some 0 values.

How can I create an array with a specific index but instead of just removing duplicates, changes it to another value,

note: it has to be done only with regular arrays and not collections since its a task in flow control.

// *Initializing first array with random numbers*
for (int i = 0; i < guesses.length; i++) {
    guesses[i] = rand.nextInt(10) + 1;
}

// *Creating a second array while removing duplicates*
int[] array2 = new int[5];
int index = 0;

lbl: for (int i = 0; i < guesses.length; i++) {
    int x = guesses[i];

    for (int j = 0; j < index; j++) {
        if (array2[j] == x) {
            continue lbl;
        }

    }
    array2[index++] = x;
}

Array with duplicates:

[6, 9, 8, 5, 5, 6]

What I get after removing duplicates (as you can see i have two 0's)

[6, 9, 8, 5, 0, 0]

example of What i need:

[6, 9, 8, 5, 3, 1]

3 Answers3

1

I think you are trying this:

guesses[]// you define

for (int i = 0; i < guesses.length; i++) {
    boolean exist = true;//we create a boolean is random number exist and start with true for while loop
   while(exist){
    exist = false;//we change it because until we didn't see the same value on array, we accept as non-exist.
    int x = rand.nextInt(10) + 1;
    
    for(int k = 0; k < i; k++){//we check everynumber until "i" we come.
     if(x == guesses[k]){//if exist we said same value exist
      exist = true; break;}
    }
    if(!exist){//if same value not exist we save it in our array
     guesses[i] = x;}
  }

}
andersen
  • 195
  • 8
0

Following the same idea you already have, your problem is that arrays in Java start with a default value, so, let's say you create an array of size 10, the array will be filled with the default value for ints (which is 0).

What you need to do is to keep an index with the "actual size" of the array, so when you add an element to the array you increase the counter, and when you remove you decrease the counter. That said, you should use this actual size to know where to put your new number.

Note that this solution doesn't need an additional array.

This approach basically uses what's known as an Array-Based List, maybe researching a little about that could help you.

Roäc
  • 149
  • 19
0

Here is one of several possible solutions.

Note that the greater the range of random numbers is to the length for the array, the less duplicates appear and the faster the algorithm will run.

Conversely, if the range of random numbers is less than the size of the array, the method will never finish. (E.g you can't fill an array of size 10 with a range of 1 to 5 and not have duplicates).

Random r = new Random();
int[] guesses = r.ints(20,1,20).toArray();
int N = guesses.length;

// Start searching for duplicates
for (int i = 0; i < N;) {
      int check = guesses[i];
      for (int x = 0; x < i; x++) {
           // check for duplicate
           // if found, assign new value and check again.
           if (guesses[x] == check) {
               guesses[x] = r.nextInt(N)+1;
               // rest i to start the outer loop anew.
               i = -1;
               break;
           }
      }
      i++;
}
          
// now sort to show distinctiveness.
Arrays.sort(guesses);
System.out.println(Arrays.toString(guesses));

For the current exercise and values, prints.

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Another way is to base the range on the maximum of the previous generated numbers. In this case, I chose start and range to generate sequential numbers so that they would be adjacent and sorted. Increasing the value of start would spread out the numbers but they would still be sorted. Note that these don't qualify as random numbers as they are being generated based on previously chosen values.

int range = 2;
int start = 1;
int[] vals = new int[20];
int max = Integer.MIN_VALUE;
for (int i = 0; i < vals.length; i++) {
    vals[i] = r.nextInt(start) + range;
    // need to maintain the max generate thus far
    // to avoid overlap and possible duplication
    max = vals[i] > max ? vals[i] : max;
    range = max+1;
}
System.out.println(Arrays.toString(vals));

Prints

[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
 
WJS
  • 36,363
  • 4
  • 24
  • 39