title: How do I remove duplicates from a C++ array?
body: <p>I have an array of structs; the array is of size N.</p>
  
  <p>I want to remove duplicates from the array; that is, do an in-place change, converting the array to have a single appearance of each struct. Additionally, I want to know the new size M (highest index in the reduced array).</p>
  
  <p>The structs include primitives so it's trivial to compare them.</p>
  
  <p>How can I do that efficiently in C++?</p>
  
  <p>I have implemented the following operators:</p>
  
  <pre><code>bool operator==(const A &amp;rhs1, const A &amp;rhs2) 
  {       
      return ( ( rhs1.x== rhs2.x )  &amp;&amp;
               ( rhs1.y == rhs2.y ) );
  }
  
  bool operator&lt;(const A &amp;rhs1, const A &amp;rhs2) 
  {       
      if ( rhs1.x == rhs2.x )  
               return ( rhs1.y &lt; rhs2.y );
  
      return ( rhs1.x &lt; rhs2.x );
  }
  </code></pre>
  
  <p>However, I get an error when running:</p>
  
  <pre><code>std::sort(array, array+ numTotalAvailable);
  
   * array will have all elements here valid.
  
  std::unique_copy(
          array, 
          array+ numTotalAvailable, 
          back_inserter(uniqueElements)); 
  
   * uniqueElements will have non-valid elements.
  </code></pre>
  
  <p>What is wrong here?</p>