#include <assert.h>

int majorityElement(int *nums, int numsSize)
{
    int candidate = nums[0];
    int vote = 1;

    for (int i = 1; i < numsSize; i++)
    {
        if (nums[i] == candidate)
            vote++;
        else
        {
            vote--;
            if (vote < 0)
            {
                candidate = nums[i];
                vote = 0;
            }
        }
    }

    return candidate;
}

int main()
{

    int test1[] = {3, 2, 3};
    assert(majorityElement(test1, 3) == 3);

    int test2[] = {2, 2, 1, 1, 1, 2, 2};
    assert(majorityElement(test2, 7) == 2);

    return 0;
}
