mirror of
https://github.com/TheAlgorithms/C.git
synced 2026-02-17 05:54:34 +00:00
21 lines
453 B
C
21 lines
453 B
C
/* Boyer-Moore Majority Vote Algorithm
|
|
* http://www.cs.utexas.edu/~moore/best-ideas/mjrty/ */
|
|
int majorityElement(int *nums, int numsSize)
|
|
{
|
|
int count = 1;
|
|
int majorNum = nums[0];
|
|
for (int i = 1; i < numsSize; i++)
|
|
{
|
|
if (count == 0)
|
|
{
|
|
majorNum = nums[i];
|
|
count++;
|
|
}
|
|
else if (majorNum == nums[i])
|
|
count++;
|
|
else
|
|
count--;
|
|
}
|
|
return majorNum;
|
|
}
|