delete duplicates from an array

I would like to delete duplicates from an array, I used vector for this problem.
I am aware that we can do two for loops but I do not want a solution in O(n^2)
my logic seems not working. What is a good way ?


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
#include<iostream>
#include <vector>

using namespace std;

int main()
{
	vector<int> A;
	int input;
	int x;
	vector<int> C;

	while(cin >>input)
	{

		cin >> input;
		A.push_back(input);

	}

	for(int i=0; i < A.size(); i++)
	{

		C.push_back(A.back());
		//check if C[i] is already in the vector:
		     //C.pop_back;


		//else:
		    //continue
	}


}

.
O( N log N ):
1
2
3
4
// http://en.cppreference.com/w/cpp/algorithm/sort
std::sort( vec.begin(), vec.end() ) ;
// http://en.cppreference.com/w/cpp/algorithm/unique
vec.erase( std::unique( vec.begin(), vec.end() ), vec.end() ) ;


O(N) time (amortised), O(N) space:
1
2
3
// http://en.cppreference.com/w/cpp/container/unordered_set
std::unordered_set<int> set( vec.begin(), vec.end() ) ;
vec = { set.begin(), set.end() } ;
Topic archived. No new replies allowed.