Checking for Duplicates in an Array Backwards
<p>This is the same person who had trouble with the last array problem just one or two days ago.
We've a new assignment which asks us to find and replace duplicates in an array of randomly generated numbers. I wrote a code and sent it to my teacher for feedback; she responded with this solution: </p>

<blockquote>
  <p>So, take the first random num and store into the first slot (this can be done before the loop).  Then, start a loop that creates the second random num and tests backwards to see if there are duplicates from the ones already stored.  So, a backwards loops that tests for duplicates and counts down to 0 from the current location and replaces duplicates. Once that test passes, then you'll go to the next element, create a new random number, and then test the ones before it for duplicates.</p>
</blockquote>

<p>I've done this here, and it's reduced the number of randomly generated numbers, but I still run into the stray duplicate: </p>

<pre><code>import java.lang.Object;
    import java.util.Random;

public class Prog433a {

    public static void main(String[]args) {

        Random randslct = new Random();

        int[] list = new int[20];
        int counter = 0;
        int index = 0;
        int min2 = 0;


        System.out.println("\nAfter");

        for (int k = 0; k &lt; list.length - 1; k++) {
            list [k] = randslct.nextInt(30) + 1;
            for (int z = list.length - 1; z &gt;= 0; z--) {
                if (list[k] == list[z] &amp;&amp; z!=k) {
                    while (list[k] == list[z]) {
                        list [k] = randslct.nextInt(30) + 1; 
                    }
                }
            }
        }

        int min = list[0];
        while (counter &lt; list.length - 1) {
            for (int x = 0; x &lt; list.length - 1; x++) { // scroll through the indexes.
                if (list[x] &lt; min) {
                    min = list[x];
                    index = x; // keep the index of the biggest number.
                } 
            }
            System.out.println(list[index]);
            min = 100 * (list[index]);

            list[index] = 100 * (list[index]); // change the value in the original array so it won't find the same max again
            counter++;
        }
    }
}
</code></pre>

<p>System Output:</p>

<pre><code>After
2
5
6
10
11
12
13
15
16
17
19
22
22
24
25
27
28
29
29


After
1
2
2
4
5
7
8
9
10
13
15
16
21
24
25
26
28
29
30

After
1
2
3
5
6
7
11
12
13
14
15
16
18
21
22
25
26
27
29

After
2
3
3
4
6
10
12
14
15
16
17
20
22
23
24
25
26
27
30

After
7
8
11
12
13
14
15
16
17
17
18
19
20
21
23
24
27
29
30
</code></pre>

<p>I posted my output towards the bottom.
Because this is an introductory coding class, I'd prefer if the solution did not involve Sets or any of the like. But alas, beggars cannot be choosers.</p>

<p>Is there something I have forgotten to add?</p>