Concise 5 lines c++ using STL functions. Any comment is welcome!

  • 0
    class Solution {
        int removeDuplicates(vector<int>& nums) {
            for(auto i=nums.begin();i!=nums.end();i++)
                auto j=find_if(i,nums.end(),bind1st(not_equal_to<int>(),*i));// find duplicates since it is sorted already.
                nums.erase(i,j-1);//remove duplicates
            return int(nums.size());

  • 0

    Because vectors use an array as their underlying storage, erasing elements in positions other than the vector end causes the container to relocate all the elements after the segment erased to their new positions.

    Which means using vector::erase makes your solution not in place.

    vector:erase - C++ Reference

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.