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.
func majorityElement(nums: [Int]) -> Int {
var records:[Int:Int] = [:]
for num in nums {
if (records[num] != nil) {
records[num] = Int(records[num]! + 1)
}else {
records[num] = 1
}
}
var max = 0
var majority = 0
for record in records {
if record.1 > max {
majority = record.0
max = record.1
}
}
return majority
}