Guest User

Untitled

a guest
Jan 18th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <queue>
  4. #include <utility>
  5. #include <map>
  6. #include <unordered_map>
  7. #include <vector>
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12. //SHOULDNT BE UNORDERED MAP BECAUSE POSITIONS ARE NOT GUARANTEED!
  13. //We store the number and its position in the array.
  14. // key: number, value: position
  15. unordered_map<int, int> numbers;
  16.  
  17. //We store the duplicated numbers and their first position.
  18. // key: number, value: position
  19. map<int, int> same;
  20.  
  21. int n; cin >> n;
  22. int pos = 0;
  23. for (size_t i = 0; i < n; i++)
  24. {
  25. int num; cin >> num;
  26. //if the number is already in the duplicated list we go on.
  27. if (same.count(num) > 0)
  28. {
  29. continue;
  30. }
  31. //If number is not entered yet we push it in the number map with its position.
  32. if (numbers.count(num) == 0)
  33. {
  34. numbers.insert(make_pair(num, pos));
  35. pos++;
  36. }
  37. else
  38. {
  39. //If its already added we get its position, delete it from the numbers map and and add it to the duplicates map.
  40. int numberPos = numbers.at(num);
  41. numbers.erase(num);
  42. same.insert(make_pair(num, numberPos));
  43. }
  44. }
  45.  
  46. //We print the numbers left in original collection.
  47. for (auto pair : numbers)
  48. {
  49. cout << pair.first << " ";
  50. }
  51.  
  52. //We print the numbers that were duplicated.
  53. for (auto pair : same)
  54. {
  55. cout << pair.first << " ";
  56. }
  57.  
  58. system("pause");
  59. return 0;
  60. }
Add Comment
Please, Sign In to add comment