Remove duplicates from a sorted array I have solved the LeetCode Remove Duplicates From Sorted Array problem:


  Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. It doesn't matter what you leave beyond the new length.


I was able to come up with a approach but only when I submitted this solution and got it accepted I found that my solution ranked somewhere around 60%, ranked by run-time. i.e) My solution was only faster that 40% of other contestants solution for the same language.  

  int removeDuplicates(vector&lt;int&gt;&amp; nums) {
    if (nums.size() == 0)
    {
        return 0;
    }
    auto start = nums.begin(), end = nums.end();
    int last_value = *(nums.end() - 1);
    for (auto it = nums.begin(); it != end;)
    {
        int find_value = *it;
        auto upper = upper_bound(start, end, find_value); //Find the last occurring index of this number 
        iter_swap(start, it);
        ++start;
        if ((upper != end) || ((upper == end) &amp;&amp; (find_value == last_value)))
        {
            it = upper;
        }
        else
        {
            ++it;
        }
    }
    return distance(nums.begin(), start);
}


Could you please suggest any performance improvements to this code. And also please go easy on the naming convention and code formatting. This was written for a programming challenge :) 

Edit.

The worst case time complexity i believe is O(n Log n) which happens when the entire array is unique. And the best case run time should be O(log n) when the entire array contains duplicates.
So the simplest solutions is to use the available stl function. However there is obviously less to learn there.

#include &lt;algorithm&gt;
#include &lt;vector&gt;

int removeDuplicates(std::vector&lt;int&gt;&amp; nums) {
    nums.erase(std::unique(nums.begin(), nums.end()), nums.end()); ;
    return nums.size();
}