JavaScript ES6 Fast Solution


  • 0
    E
    const majorityElement = function(nums) {
        let ans 
        let n = nums.length / 2
        let obj = nums.reduce((prev, curr) => (prev[curr] = ++prev[curr] || 1, prev), {})
        Object.keys(obj).forEach(function (key) {
            if (obj[key] > n) {
                ans = parseInt(key)
            }
        })
        return ans
    };
    

Log in to reply
 

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