Menu Close

Find the majority element appearing more than n/2 times

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.

Input:
1 1 2 1 3 5 1
Output:
1
def findMajor(Array_nums,N):
    L=[]
    for i in range(N):
        Occ=Array_nums.count(Array_nums[i])
        if Occ > N/2 :
            L.append(Array_nums[i])
    print(*list(set(L)))

# Driver code
if __name__ == "__main__":
    arr = list(map(int,input('Array : ').split(' ')))  #or arr = [1, 1, 2, 1, 3, 5, 1]
    n = len(arr)
    findMajor(arr, n)

Input_1:
Array : 3 3 4 2 4 4 2 4 4
Output:
4


Input_2:
Array : 1 1 2 1 3 5 1
Output:
1


Input_3:
Array : 3 2 3
Output:
3


Executed using python3 (Linux)

More Q