GFG

Majority Element


Difficulty: Medium Accuracy: 27.82% Submissions: 715K+ Points: 4 Average TIme: 59m

Given an array arr[]. Find the majority element in the array. If no majority element exists, return -1.


Note: A majority element in an array is an element that appears strictly more than arr.size()/2 times in the array.


Examples:


Input: arr[] = [1, 1, 2, 1, 3, 5, 1]

Output: 1

Explanation: Since, 1 is present more than 7/2 times, so it is the majority element.


Input: arr[] = [7]

Output: 7

Explanation: Since, 7 is single element and present more than 1/2 times, so it is the majority element.


Input: arr[] = [2,13]

Output: -1

Explanation: Since, no element is present more than 2/2 times, so there is no majority element.


Constraints:

1 ≤ arr.size() ≤ 105

0 ≤ arr[i] ≤ 105





If you are facing any issue on this page. Please let us know.





Start Timer
  1. #User function template for Python 3
  2. class Solution:
  3. def majorityElement(self, arr):
  4. #code here
  5. Driver Code Ends