#include <iostream>
#include <vector>
using namespace std;
vector<int> findDup2(int a[], int len) {
vector<int> ret;
for(int i = 0; i < len; i++) {
while(a[a[i]] != a[i]) {
swap(a[a[i]], a[i]);
}
}
for (int i = 0; i<len; i++) {
if (a[i] != i && a[a[i]] == a[i]) {
ret.push_back(a[i]);
a[a[i]] = i;
}
}
return ret;
}
int main() {
int a[] = {4, 1, 1, 1, 3, 4};
vector<int> vet;
vet = findDup2(a, sizeof(a)/sizeof(a[0]));
for (int i:vet) cout<<i<<" ";
cout << endl;
// your code goes here
int b[] = {0, 1, 0, 1, 2, 2, 4, 1};
vet = findDup2(b, sizeof(b)/sizeof(b[0]));
for (int i:vet) cout<<i<<" ";
cout<<endl;
int c[] = {1, 1, 1, 1, 2, 2, 2, 3, 3, 3};
vet = findDup2(c, sizeof(c)/sizeof(c[0]));
for (int i:vet) cout<<i<<" ";
cout<<endl;
int d[] = {1, 2, 1, 2, 1, 3, 3, 1, 3, 2};
vet = findDup2(d, sizeof(d)/sizeof(d[0]));
for (int i:vet) cout<<i<<" ";
cout<<endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdmVjdG9yPGludD4gZmluZER1cDIoaW50IGFbXSwgaW50IGxlbikgewoJdmVjdG9yPGludD4gcmV0OwoJCglmb3IoaW50IGkgPSAwOyBpIDwgbGVuOyBpKyspIHsKCQl3aGlsZShhW2FbaV1dICE9IGFbaV0pIHsKCQkJc3dhcChhW2FbaV1dLCBhW2ldKTsKCQl9Cgl9CgkKCWZvciAoaW50IGkgPSAwOyBpPGxlbjsgaSsrKSB7CgkJaWYgKGFbaV0gIT0gaSAmJiBhW2FbaV1dID09IGFbaV0pIHsKCQkJcmV0LnB1c2hfYmFjayhhW2ldKTsKCQkJYVthW2ldXSA9IGk7CgkJfQoJfQoJcmV0dXJuIHJldDsKfQoKaW50IG1haW4oKSB7CglpbnQgYVtdID0gezQ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