Największa trudność w tym rozwiązaniu to brak możliwości posortowania.
Jeśli pytasz o wskazówki to:
1. Zadeklaruj kontener unordered_map
2. Przeglądaj element po elemencie podanej tablicy.
2. Inkrementuj licznik dla danego elementu w unordered_map.
3. Usuń wszystkie elementy występujące w unordered_map z licznikiem większym niż 1 z tablicy.
Poniżej przykład jeśli nie poradzisz sobie sam:
#include <unordered_map>
#include <iostream>
template<typename Iterator>
void showTable(const Iterator first, const Iterator last) {
for(auto it = first; it != last; ++it) {
std::cout << *it << ' ';
}
std::cout << '\n';
}
template<typename Iterator>
auto countElements(const Iterator first, const Iterator last) {
std::unordered_map<int, size_t> valuesMap;
std::for_each(first, last, [&](int value) { ++valuesMap[value];});
return valuesMap;
}
int main() {
int table[] = { 10, 33, 26, 10, 54, 66, 33, 12, 54, 1, 7 };
auto first = std::cbegin(table);
auto last = std::cend(table);
std::cout << "Podana tabela : ";
showTable(first, last);
auto valuesMap = countElements(first, last);
// Tu masz zasadniczy algorytm usunięcia...
auto endIter = std::remove_if(std::begin(table), std::end(table),
[&valuesMap](int value) {
return valuesMap[value] > 1 ? true: false;
});
std::cout << "Elementy unikalne: ";
showTable(std::begin(table), endIter);
}