Remove Duplicates
Completed
Read n integers, sort them (with a comparator) and remove duplicates with unique. Print the distinct values and how many there are.
Input
Line 1: n. Line 2: n integers.
Output
Two lines.
Example
Input
8 3 1 3 2 1 5 3 2
Output
1 2 3 5 4
Editor
C++
Hints
unique only removes adjacent duplicates, so sort first.
Solution explanation
Sort + unique + erase is the classic way to deduplicate a vector.
Show solution
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v(n);
for (int &x : v) cin >> x;
sort(v.begin(), v.end(), less<int>());
v.erase(unique(v.begin(), v.end()), v.end());
for (size_t i = 0; i < v.size(); i++) cout << v[i] << (i + 1 < v.size() ? " " : "\n");
cout << v.size() << "\n";
return 0;
}