class Solution {
public:
int removeDuplicates(vector& nums) {
nums.erase(unique(nums.begin(),nums.end()),nums.end());
return nums.size();
}
};
How unique function is working in this problem's solution
Hey @gaurav19063 , What unique function in c++ vector does is , it takes all the unique elements in the vector and put it first in the vector itself . Now the elements which were repeated are put after the last index of unique numbers .
For ex:
vector<int> v{1,2,3,1,2,3,3,4,5,4,5,6,7}; sort(v.begin(), v.end()); auto last = unique(v.begin(), v.end()); // v now holds {1 2 3 4 5 6 7 x x x x x x}, where 'x' is indeterminate v.erase(last, v.end());
Hope it helps .
If your doubt is now resolved please mark it as resolved and rate me accordingly .
Else you can further questions here .
Thankyou