Challenge: Unique Visitors
Completed
Read n and then n numbers (visitor IDs). Print how many different IDs there were, then all the different IDs in increasing order on one line.
Input
n, then n integers.
Output
3 4 7 9
Example
Input
5 9 4 7 4 9
Output
3 4 7 9
Editor
C++
Hints
Insert everything into a set<int>: duplicates disappear.
A set loops in increasing order.
Solution explanation
A set removes duplicates and sorts the values for you.
Show solution
#include <iostream>
#include <set>
using namespace std;
int main() {
int n;
cin >> n;
set<int> ids;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
ids.insert(x);
}
cout << ids.size() << "\n";
bool first = true;
for (int id : ids) {
if (!first) cout << " ";
cout << id;
first = false;
}
cout << "\n";
return 0;
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module