title: How do I print out the first instance of duplicates in an array of randomly generated numbers?
body: <p>I've printed 10 random numbers in the range 20-50 while storing them in an array. I'm having trouble getting the duplicates found to work. If there's an instance in the array where there's a duplicate found, I'm trying to print out the position where it was found, not the subscript.</p>
  
  <p>For example:</p>
  
  <p>46</p>
  
  <p>24</p>
  
  <p>46</p>
  
  <p>48</p>
  
  <p>44</p>
  
  <p>20</p>
  
  <p>24</p>
  
  <p>46</p>
  
  <p>44</p>
  
  <p>27</p>
  
  <p>First pair of duplicates were found at positions: 1 and 3</p>
  
  <p>Is the output that I'm trying to achieve if there's duplicates and then the same except "No duplicates were generated." if there wasn't any.</p>
  
  <pre><code>import javax.swing.JOptionPane;
  public class sheet11t1
  {
      public static void main(String[] args)
      {
          String results = "";
          int numbers[] = new int[10];
          int j;
          for(int i = 0; i &lt; numbers.length; i++)
          {
              numbers[i] = (int) ((Math.random() * 31) + 20);
              results += i + "\n";
          }
          boolean duplicateFound = false;
          for(int i = 0; i &lt; numbers.length - 1 &amp;&amp; !duplicateFound; i++)
          {
              for(j = i + 1; j &lt; numbers.length &amp;&amp; !duplicateFound; j++)
              {
                  if(numbers[i] == numbers[j])
                      duplicateFound = true;
              }
          }
          if(duplicateFound)
                  results += "First pair of duplicates were found at positions: " + numbers[i + 1] + " and " + numbers[j + 1];
          else
                  results += "No duplicates were generated.";
          JOptionPane.showMessageDialog(null, results);
      }
  }
  </code></pre>