Javascript solution


  • 0
    T
    /**
     * @param {number[]} nums
     * @return {number[]}
     */
    var singleNumber = function(nums) {
        var number = {};
        
        for(var i=0;i<nums.length;i++) {
            var key = nums[i].toString();
            if(number.hasOwnProperty(key)) {
                delete number[key];
            } else {
                number[key] = true;
            }
        }
        
        var result = Object.getOwnPropertyNames(number);
        result[0] = Number(result[0]);
        result[1] = Number(result[1]);
        
        return result;
    };

  • 0
    N

    Yes this runs in linear time, but can have up to O(n) memory usage probably averaging around O(log n) for memory. If you use the xor methods you could achieve around O(n2 + k) and O(1) memory.


Log in to reply
 

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