Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <set>
- #include <queue>
- #include <utility>
- #include <map>
- #include <unordered_map>
- #include <vector>
- using namespace std;
- int main()
- {
- //SHOULDNT BE UNORDERED MAP BECAUSE POSITIONS ARE NOT GUARANTEED!
- //We store the number and its position in the array.
- // key: number, value: position
- unordered_map<int, int> numbers;
- //We store the duplicated numbers and their first position.
- // key: number, value: position
- map<int, int> same;
- int n; cin >> n;
- int pos = 0;
- for (size_t i = 0; i < n; i++)
- {
- int num; cin >> num;
- //if the number is already in the duplicated list we go on.
- if (same.count(num) > 0)
- {
- continue;
- }
- //If number is not entered yet we push it in the number map with its position.
- if (numbers.count(num) == 0)
- {
- numbers.insert(make_pair(num, pos));
- pos++;
- }
- else
- {
- //If its already added we get its position, delete it from the numbers map and and add it to the duplicates map.
- int numberPos = numbers.at(num);
- numbers.erase(num);
- same.insert(make_pair(num, numberPos));
- }
- }
- //We print the numbers left in original collection.
- for (auto pair : numbers)
- {
- cout << pair.first << " ";
- }
- //We print the numbers that were duplicated.
- for (auto pair : same)
- {
- cout << pair.first << " ";
- }
- system("pause");
- return 0;
- }
Add Comment
Please, Sign In to add comment