title: Removing duplicate entries in an array (C++)
body: <p>I'm having an issue in which a function that in theory should remove all duplicate values from an array doesn't work. Here's how it works:</p>
  
  <ol>
  <li>I have two arrays, and then I populate them with random numbers
  between 0 and 50 inclusive. </li>
  <li>I sort the array values in order using a sort function</li>
  <li>I then run my dedupe function</li>
  <li>I sort the array values in order again</li>
  <li>I then output the values in both arrays</li>
  </ol>
  
  <p>The problem is, the loop in the dedupe function is ran 19 times regardless of how many duplicate entries it finds, which is extremely strange. Also, it still gives duplicates.</p>
  
  <p>Any ideas? Thanks!</p>
  
  <pre><code>int* dedupe(int array[ARRAY_SIZE])      //remove duplicate array values and replace with new values.
  {   bool dupe = false;
      while(dupe!=true)
      {   
          for(int j=0; j&lt;ARRAY_SIZE; j++)
          {   if(array[j] == array[j+1])
              {   array[j] = rand(); 
                  array[j] = array[j] % 51;
                  dupe = false;
              }
              else { dupe = true; // the cout part is for debugging
                      cout &lt;&lt; dupe &lt;&lt; endl; }
          }
      } return array;
  }
  int main()
  {
      int a[9], b[9];
      srand(time(0));
      populate(b);
      populate(a);
      sort(a,ARRAY_SIZE);
      sort(b,ARRAY_SIZE);
      dedupe(a);
      dedupe(b);
      sort(a,ARRAY_SIZE);
      sort(b,ARRAY_SIZE);
      for(int i=0; i&lt;10; i++)
      {   cout &lt;&lt; "a[" &lt;&lt; i &lt;&lt; "] = " &lt;&lt; a[i] &lt;&lt; "\t\t" &lt;&lt; "b[" &lt;&lt; i &lt;&lt; "] = " &lt;&lt; b[i] &lt;&lt; endl; }
      return 0;
  }
  </code></pre>
  
  <p>Nothing suggested so far has solved the problem. Does anyone know of a solution?</p>