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) ret.push_back(a[i]);
  16. }
  17. return ret;
  18. }
  19.  
  20. int main() {
  21. int a[] = {4, 1, 1, 1, 3, 4};
  22. vector<int> vet;
  23. vet = findDup2(a, sizeof(a)/sizeof(a[0]));
  24. for (int i:vet) cout<<i<<" ";
  25. cout << endl;
  26. // your code goes here
  27.  
  28. int b[] = {0, 1, 0, 1, 2, 2, 4, 1};
  29. vet = findDup2(b, sizeof(b)/sizeof(b[0]));
  30. for (int i:vet) cout<<i<<" ";
  31. return 0;
  32. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
1 1 4 
1 2 0 1