Solution beating 100% - Javascript


  • 0
    R

    1.- Look for the number in the index of the array marked by the current position

    2.- If the number is negative it means that it is repeated

    var findDuplicates = function(nums) {
        var res = [],
            index,
            i;
        
        for(i = 0; i < nums.length; i++){
            index = Math.abs(nums[i]) - 1;
            
            if(nums[index] < 0)
                res.push(index + 1);
            else    
                nums[index] *= -1;
        }
        
        return res;
    };

Log in to reply
 

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