A solution for Single Element in a Sorted Array in Javascript


  • 0
    J

    When I tried to resolve the problem "540.Single Element in a Sorted Array" in Javascript, it did work in my web browser, but leetcode.com showed Internal Error. So I clicked Solution button and it showed that they didn't have an answer at that moment.Here is my solution:

    var singleNonDuplicate = function(nums) {
        var dict ={}
        for (var i = 0; i < nums.length; i ++){
            if(dict[nums[i]] == undefined){
                dict[nums[i]] = 1
            }else {
                dict[nums[i]] ++
            }
        }
        for (var o in dict) {
            if (dict[o] === 1){
                return new Number(o)
            }
        }
    };

Log in to reply
 

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