c++数组去重并打印
时间: 2023-12-20 13:31:44 浏览: 93
26删除有序数组中的重复项.zip
以下是一种C++数组去重并打印的方法:
```cpp
#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
unordered_set<int> uniqueElements;
for (int i = n - 1; i >= 0; i--) {
if (uniqueElements.find(arr[i]) == uniqueElements.end()) {
uniqueElements.insert(arr[i]);
}
}
cout << uniqueElements.size() << endl;
for (auto it = uniqueElements.begin(); it != uniqueElements.end(); it++) {
cout << *it << " ";
}
cout << endl;
return 0;
}
```
阅读全文