Timeline
Timeline
2025-09-28
init
std::unique
Problem:
std::unique is a C++
1234567891011 | using std::vector;class Solution {public: int removeDuplicates(vector<int>& nums) { nums.erase(std::unique(nums.begin(),nums.end()),nums.end()); return nums.size(); }}; |
Classic fast-slow pointer solution: the fast pointer represents the index position reached while traversing the array, and the slow pointer represents the index position where the next different element should be filled. Initially, both pointers point to index 1.
123456789101112131415161718 | class Solution {public: int removeDuplicates(vector<int>& nums) { int n = nums.size(); if (n == 0) { return 0; } int fast = 1, slow = 1; while (fast < n) { if (nums[fast] != nums[fast - 1]) { nums[slow] = nums[fast]; ++slow; } ++fast; } return slow; }}; |
