title: Java - Listing the index of duplicate values in an int array
body: <p>I have to locate and list all duplicate index values in array.</p>
  
  <p>Example:
  int[] array = { 0, 7, 9, 1, 5, 8, 7, 4, 7, 3};</p>
  
  <p>7 is located in three different locations at index 1, 6, and 8. How would I go about modifying my existing code in order to have outputResults.setText() show the location of the duplicate values? outputResults.setText() is JTextField if that helps.</p>
  
  <pre><code>String tmp1 = getNumbers.getText();
      try {
          int search = Integer.parseInt(tmp1);
          for (p = 0; p &lt; array.length; p++) {
              if(array[p]==search) {
                  b = true;
                  index = p;
              }
          }   
          if(b==true)
              outputResults.setText(search + " was in the following fields of the array " + index);
           else 
              throw new NumberNotFoundException("Your number was not found.");
  
  
      } catch (NumberFormatException ex) {
          JOptionPane.showMessageDialog(getContentPane(), "You can only search for integers.");
  
      } catch (NumberNotFoundException ex) {
          JOptionPane.showMessageDialog(getContentPane(), ex.getMessage());
      }
  </code></pre>
  
  <p>At it's current state, it will only list the last time the duplicate number was located which would be index 8 based on my example. The list of numbers in the array is inputted by the user, and I'm not allowed to sort the values. My original guess was to create a nested loop and whenever it located an duplicated number, add p (current index it's searching) to a new array. I would then list the full array in outputResults.setText() but it gave several warnings and errors when I tried.</p>
  
  <p>The full code can be found here if needed: <a href="http://pastebin.com/R7rfWAv0" rel="nofollow">http://pastebin.com/R7rfWAv0</a>
  And yes, the full program is a mess but it gets the job done and I was having such a headache with it. Also note, in the full program, the professor asked us to throw an exception if a duplicate value was detected as extra credit. I did it, but I commented it out to finish the original assignment so please disregard it.</p>