JS Solution using a map


  • 0
    S
    var findDisappearedNumbers = function(nums) {
    let map = {};
    nums.forEach((n,idx)=>{
        map[Number(idx+1)] = idx+1;
    });
    
    nums.forEach((n,idx)=>{
        delete map[n];
    });
    
    return Object.values(map);
    

    };


  • 0
    S

    @shlomi.schwartz An improvement with one iteration and a second map:

    var findDisappearedNumbers = function (nums) {
    let missing = {};
    let banned = {};
    
    for (let i = 0; i < nums.length; i++) {
        if (!banned[i + 1]) {
            missing[i + 1] = i + 1;
        }
        banned[nums[i]] = true;
        delete missing[nums[i]];
    }
    
    return Object.values(missing);
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.