Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
You may assume that the array is non-empty and the majority element always exist in the array.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
var v = 0, res = 0 var dict = [Int: Int]() nums.forEach { var t = 1 if let n = dict[$0] { t = n + 1 } if t > v { res = $0 v = t } dict[$0] = t } return res |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼