-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path26.txt
30 lines (27 loc) · 829 Bytes
/
26.txt
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
Remove Deuplicates from Sorted Array
Given an array in non-decreasing order you need to remove the duplicates in-place
If it was not in-place then we could take a map and keep track of count and then according to that modify the element of array
OR
We could track the count of duplicates and then use that count to keep a pointer over i-count to store the unique elements
int removeDuplicates(vector<int>& nums) {
int count=0;
for(int i=1;i<nums.size();i++){
if(nums[i]==nums[i-1]){
count++;
}
else{
nums[i-count]=nums[i];
}
}
return nums.size()-count;
// OR If not in-place
// map<int,int> mp;
// for(auto n:nums){
// mp[n]++;
// }
// int i=0;
// for(auto m:mp){
// nums[i++]=m.first;
// }
// return i;
}