LC 169. Majority Element

/**
 * @param {number[]} nums
 * @return {number}
 */
var majorityElement = function (nums) {
  let half = nums.length / 2
  let map = new Map()

  for (let num of nums) {
    if (map.has(num)) {
      let currNum = map.get(num)
      map.set(num, currNum + 1)
    } else {
      map.set(num, 1)
    }
    if (map.get(num) > half) return num
  }
};
欢迎通过「邮件」或者点击「这里」告诉我你的想法
Welcome to tell me your thoughts via "email" or click "here"