23. leetcode 169. Majority Element

169. Majority Element

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.

思路1:hash表

思路2:

此题目是求数组中出现超过一半以上次数的数,所以设置一个element存储当前出现次数最多的数,设置count记录它当前出现的频数,每遇到一个和element不同的数就将count减一。

 

posted @ 2017-04-09 21:40  蓦然闻声  阅读(98)  评论(0编辑  收藏  举报