Leetcode 442 Solution
This article provides solution to leetcode question 442 (find-all-duplicates-in-an-array)
Access this page by simply typing in "lcs 442" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/find-all-duplicates-in-an-array
Solution
class Solution {
public:
vector<int> findDuplicates(vector<int>& nums) {
vector<int> res;
for (int i = 0; i < nums.size(); i++)
{
int target = abs(nums[i]) - 1;
if (nums[target] < 0)
res.push_back(target + 1);
else
nums[target] = -nums[target];
}
return res;
}
};