O(n) solution?


  • 0
    Z

    This is JavaScript solution, please let me know if you know this is O(n) time or not? Thanks!

    /**
     * @param {number[]} nums
     * @return {number}
     */
    var removeDuplicates = function(nums) {
        //map key-num, value-count
        // count=2{looptonext}else1{map[]=2}else{map[]=1};
        var map = {}, rst=[];
        for(var i = 0; i < nums.length; i++){
            if(map[nums[i]] === 1){
                rst.push(nums[i]);
                map[nums[i]] = 2;
            }else if(!map[nums[i]]){
                rst.push(nums[i]);
                map[nums[i]] = 1;
            }
            nums[rst.length-1] = rst[rst.length-1];
        }
        return rst.length;
    };
    

Log in to reply
 

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