def majority_element(nums):
    """
    Find the majority element in given array

    This algorithm loops through each element and maintains a count of
    nums[maj_index]. If the next element is same then increment the count,
    if the next element is not same then decrement the count, and if the
    count reaches 0 then changes the maj_index to the current element and
    set the count again to 1. So, the algorithm gives us  a candidate element.

    :param nums: list[int]
    :type nums: given array of numbers
    :return: the majority element
    :rtype: int
    """
    if len(nums) == 0:
        return -1

    k, count = -1, 0
    for n in nums:
        if count == 0:
            k = n
        if k == n:
            count += 1
        else:
            count -= 1
    return k