How to make a vector unique (remove duplicate elements) in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <algorithm>

using std::cout;
using std::endl;
using std::vector;

int main()
{
	vector<int> vec{ 1, 1, 3, 3, 3, 4, 5, 7, 3 };

	sort(vec.begin(), vec.end());
	vec.erase(unique(vec.begin(), vec.end()), vec.end());

	for (int val : vec)
		cout << val << " ";
	
	cout << endl;

	return 0;
}

/*
run:

1 3 4 5 7

*/

 



answered May 1, 2018 by avibootz

Related questions

1 answer 131 views
131 views asked Feb 21, 2022 by avibootz
1 answer 172 views
1 answer 96 views
...