Leetcode 442 Solution

This article provides solution to leetcode question 442 (find-all-duplicates-in-an-array)

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;
    }
};