def find_majority_element(nums): # dictionary to store the frequencies of each element records = {} for item in nums: # if current element is already present # in records dictionary then increment it by 1 if item in records: records[item] += 1 # else count its first occurrence else: records[item] = 1 # return the key with the maximum value return max(records.keys(), key=records.get) # Driver code def main(): nums = [[2, 2, 3, 2, 2, 1, 2, 3], [3, 2, 3, 3], [3, 11, 10, 11, 11, 14, 11, 10, 11, 11], [1, 2, 1, 2, 1, 1, 1, 1, 3, 2, 2, 2, 1, 1], [5, 2, 1, 5, 5, 5, 5] ] for i in range(len(nums)): print(str(i+1)+".\t The majority element in", nums[i],"is:", find_majority_element(nums[i])) print("-"*100) if __name__ == '__main__': main()