-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ1.cpp
More file actions
40 lines (30 loc) · 809 Bytes
/
Copy pathQ1.cpp
File metadata and controls
40 lines (30 loc) · 809 Bytes
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
#include <iostream>
#include <vector>
int removeDuplicates(std::vector<int>& nums) {
if (nums.empty()) {
return 0;
}
int index = 0;
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] != nums[index]) {
nums[++index] = nums[i];
}
}
return index + 1;
}
int main() {
std::vector<int> nums = {1, 1, 2, 2, 3, 4, 4, 5, 5};
std::cout << "original array: ";
for (int num : nums) {
std::cout << num << " ";
}
std::cout << std::endl;
int newLength = removeDuplicates(nums);
std::cout << "array after removing duplicates: ";
for (int i = 0; i < newLength; ++i) {
std::cout << nums[i] << " ";
}
std::cout << std::endl;
std::cout << "new length: " << newLength << std::endl;
return 0;
}