#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) ret.push_back(a[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<<" ";
	return 0;
}