Given that each element is between 1 and n. All the elements occur either once or twice.

So, in first pass, we can swap all the elements to their respective positions.

In second pass, all the one-time occuring elements are supposed to be at their positions. The elements that violate this conditions are the elements of our interest (because they are at some other positions, in addition of being at their respective index, where they are not supposed to be).

```
class Solution {
public:
vector<int> findDuplicates(vector<int>& nums) {
if(nums.empty()) return {};
vector<int> res;
int n = nums.size();
for(int i = 0; i < n; i++)
nums[i]-=1; //for indexing flexibility
int i = 0;
while(i < n) {
if(nums[i] != nums[nums[i]])
swap(nums[i], nums[nums[i]]); //swap elements to their respective indexes.
else
i++;
}
for(int i = 0; i < n; i++) {
if(nums[i] != i)
res.push_back(nums[i]+1); //get the elements which are at some other indexes (as they are occuring twice)
}
return res;
}
};
```