clueless coding // TODO: be smarter

LeetCode Find All Numbers Disappeared in an Array: CPP Solution



TL;DR Code Solution


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