mirror of
https://github.com/Manoj-HV30/dsa-competitive-programming.git
synced 2026-05-16 19:35:22 +00:00
26 lines
843 B
C++
26 lines
843 B
C++
class Solution{
|
|
public:
|
|
int removeDuplicates(vector<int> & nums){
|
|
if(nums.empty()) return 0;
|
|
int i =1;
|
|
for(int j =1;j<nums.size();j++){
|
|
if(nums[j]!=nums[i-1]){
|
|
nums[i] = nums[j];
|
|
i++;
|
|
}
|
|
}
|
|
return i;
|
|
|
|
}
|
|
};
|
|
|
|
//TC : O(n)
|
|
//SC: O(1)
|
|
|
|
/* InPlace algorithm : An algorithm that modifies the input data directly, uses constant space o(1). Here We traverse the sorted array and keep only unique elements by overwriting duplicates in-place.
|
|
Whenever a new (different) element is found, we place it at the next available position.
|
|
The final count of unique elements is returned.
|
|
i -> marks the position where the next unique element should be stored
|
|
j-> scans the array to find new elements
|
|
*/
|