LC 169. Majority Element

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
/**
 * @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
  }
};





评论和交流请发送邮件到 me@tianhegao.com





通过以下渠道赞赏此文