-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathremove_duplicate elements.cpp
More file actions
40 lines (34 loc) · 1.17 KB
/
remove_duplicate elements.cpp
File metadata and controls
40 lines (34 loc) · 1.17 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
#include <iostream>
#include<vector>
#include <set>
using namespace std;
vector<int> removeduplicate(vector<int>input) //function to return vector
{
set<int>s; // using set because it only stores unique elements
vector<int>result;
for(int i=0;i<input.size();i++)
{
if(s.find(input[i])==s.end()) //it checks the set .here find() acts as an iterator and if it reaches end of the set that
// means the element is not present inside the set.(find() points to the end)
{
s.insert(input[i]);
result.push_back(input[i]); //push_back is used because we dont know the size of the result vector
}
}
return result;
}
int main()
{
vector<int>input(5);
for(int i=0;i<input.size();i++)
{
cin>>input[i]; //input[] is used and not push_back because we know the size of the input vector
}
vector<int>result;
result=removeduplicate(input);
for(int i=0;i<result.size();i++)
{
cout<<result[i]<<endl;
}
return 0;
}