title: How to remove duplicates from a list using an auxiliary array in Java?
body: <p>I am trying to remove duplicates from a list by creating a temporary array that stores the indices of where the duplicates are, and then copies off the original array into another temporary array while comparing the indices to the indices I have stored in my first temporary array.</p>
  
  <pre><code>public void removeDuplicates()
  {
      double tempa [] = new double [items.length];
      int counter = 0;
      for ( int i = 0; i&lt; numItems ; i++)
      {
          for(int j = i + 1; j &lt; numItems; j++)
          {
              if(items[i] ==items[j])
              {
                  tempa[counter] = j;
                  counter++;
  
              }
          }
      }
  
      double tempb [] = new double [ items.length];
      int counter2 = 0;
      int j =0;
      for(int i = 0; i &lt; numItems; i++)
      {
          if(i != tempa[j])
          {
              tempb[counter2] = items[i];
              counter2++;
  
          }
          else
          {
              j++;
  
          }
      }
  
      items = tempb;
      numItems = counter2;
  }
  </code></pre>
  
  <p>and while the logic seems right, my compiler is giving me an arrayindexoutofbounds error at</p>
  
  <pre><code>tempa[counter] = j;
  </code></pre>
  
  <p>I don't understand how counter could grow to above the value of items.length, where is the logic flaw?</p>