-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDuplicatesFromSortedArray.cpp
More file actions
49 lines (43 loc) · 1.04 KB
/
RemoveDuplicatesFromSortedArray.cpp
File metadata and controls
49 lines (43 loc) · 1.04 KB
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
/* https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/727/
#array #hash-table #map
*/
#include<iostream>
#include<map>
#include<vector>
using namespace std;
// Implement with unsorted / sorted Array with O(n)
int removeDuplicates(vector<int>& nums) {
map<int,int> hashMap;
vector<int>::iterator it_nums = nums.begin();
while(it_nums != nums.end()) {
map<int,int>::iterator it_map = hashMap.find(*it_nums);
if ( it_map == hashMap.end()) {
hashMap.insert(pair<int,int>(*it_nums, 1));
++it_nums;
} else {
nums.erase(it_nums);
}
}
return nums.size();
}
//test
int main() {
vector<int> vt;
vt.push_back(0);
vt.push_back(0);
vt.push_back(0);
vt.push_back(1);
vt.push_back(1);
vt.push_back(1);
vt.push_back(2);
vt.push_back(2);
vt.push_back(2);
vt.push_back(3);
vt.push_back(3);
vt.push_back(4);
cout << removeDuplicates(vt) << endl;
for (int i = 0; i < vt.size(); i++)
cout << vt.at(i) << " ";
cout <<endl;
return 0;
}