JavaScript simple solution, by using object and Array.splice


  • 0
    L

    var hasTwice = {},
    result = 0;
    for (var i = 0; i < nums.length; i++){
    if (hasTwice[nums[i]] === undefined){
    hasTwice[nums[i]] = 1;
    }else if(hasTwice[nums[i]] === 1){
    hasTwice[nums[i]]++;
    }else{
    nums.splice(i, 1);
    i--;
    }
    }
    for (var j in hasTwice){
    result += hasTwice[j];
    }

    return result;

Log in to reply
 

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