clueless coding // TODO: be smarter

LeetCode Find All Duplicates in an Array: CPP Solution



TL;DR Code Solution


vector<int> findDuplicates(vector<int>& nums) {

    vector<int> result;
    for (int i = 0; i < nums.size(); i++) {
        int index = abs(nums[i]) - 1;
        if (nums[index] < 0) {
            result.push_back(index + 1);
        }
        nums[index] = -1 * nums[index];
    }
    return result;

}