-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove-duplicates.cpp
50 lines (45 loc) · 1.07 KB
/
remove-duplicates.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
Since array is sorted we can compare the consecutive elements.
We will keep a pointer, and every distinct consecutive pair we will increment the pointer.
It simply refers where to override the duplicate item with next distinct one.
Time complexity O(n)
Space complexity O(1)
*/
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.size() == 1){
return 1;
}
if(nums.size() == 2){
if(nums[0] == nums[1]){
return 1;
}
return 2;
}
int j = 1;
for(int i = 0; i < nums.size() - 1; i++){
if(nums[i] != nums[i + 1]){
nums[j] = nums[i + 1];
j++;
}
}
return j;
}
};
/*
cleaner version
*/
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int j = 1;
for(int i = 1; i < nums.size(); i++){
if(nums[i - 1] != nums[i]){
nums[j] = nums[i];
j++;
}
}
return j;
}
};