fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. vector<int> findDup2(int a[], int len) {
  6. vector<int> ret;
  7.  
  8. for(int i = 0; i < len; i++) {
  9. while(a[a[i]] != a[i]) {
  10. swap(a[a[i]], a[i]);
  11. }
  12. }
  13.  
  14. for (int i = 0; i<len; i++) {
  15. if (a[i] != i && a[a[i]] == a[i]) {
  16. ret.push_back(a[i]);
  17. a[a[i]] = i;
  18. }
  19. }
  20. return ret;
  21. }
  22.  
  23. int main() {
  24. int a[] = {4, 1, 1, 1, 3, 4};
  25. vector<int> vet;
  26. vet = findDup2(a, sizeof(a)/sizeof(a[0]));
  27. for (int i:vet) cout<<i<<" ";
  28. cout << endl;
  29. // your code goes here
  30.  
  31. int b[] = {0, 1, 0, 1, 2, 2, 4, 1};
  32. vet = findDup2(b, sizeof(b)/sizeof(b[0]));
  33. for (int i:vet) cout<<i<<" ";
  34. cout<<endl;
  35.  
  36. int c[] = {1, 1, 1, 1, 2, 2, 2, 3, 3, 3};
  37. vet = findDup2(c, sizeof(c)/sizeof(c[0]));
  38. for (int i:vet) cout<<i<<" ";
  39. cout<<endl;
  40.  
  41. int d[] = {1, 2, 1, 2, 1, 3, 3, 1, 3, 2};
  42. vet = findDup2(d, sizeof(d)/sizeof(d[0]));
  43. for (int i:vet) cout<<i<<" ";
  44. cout<<endl;
  45.  
  46. return 0;
  47. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
1 4 
1 2 0 
1 2 3 
1 2 3