clueless coding // TODO: be smarter

LeetCode Set Mismatch: CPP Solution



TL;DR Code Solution


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

    unordered_set<int> set;
    int duped;
    int sum = 0;
    for (int i = 0; i < nums.size(); i++) {
        if (set.find(nums[i]) != set.end()) {
            duped = nums[i];
        }
        else {
            set.insert(nums[i]);
        }
        sum += nums[i];
    }
    int n = nums.size();
    vector<int> result = {duped, (n * (n+1) / 2) - sum + duped};
    return result;

}