O(n) time JavaScript solution


  • 0
    R
    var majorityElement = function(nums) {
        var map = {};
        
        for (var i = 0; i < nums.length; i++) {
            var times = map[nums[i]] || 0;
            if ((times + 1) > parseInt(nums.length/2, 10)) {
                return nums[i];
            }
            map[nums[i]] = times + 1;
        }
    };

Log in to reply
 

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