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 int majorityElement(int* nums, int numsSize) {
 2     int i;
 3     int flag;
 4     int count = 0;
 5     for(i = 0; i < numsSize; i++)
 6     {
 7         if(0 == count)
 8         {
 9             flag = nums[i];
10             count = 1;
11         }
12         else
13         {
14             if(nums[i] != flag)
15                 count--;
16             else
17                 count++;
18         }
19     }
20     return flag;
21 }
原文地址:https://www.cnblogs.com/boluo007/p/5483662.html