25. Majority Element
The problem can be found at the following link: 🔗 Question Link
🧩 Problem Description
Given an array arr[], find the majority element in the array. A majority element is an element that appears strictly more than arr.size() / 2 times.
If no such element exists, return -1.
📘 Examples
Example 1:
Input:
arr[] = [1, 1, 2, 1, 3, 5, 1]
Output:
1
Explanation:
1 appears 4 times out of 7, which is more than 7/2. Hence, it is the majority element.
Example 2:
Input:
arr[] = [7]
Output:
7
Explanation:
The only element 7 appears once, which is more than 1/2. Hence, it is the majority element.
Example 3:
Input:
arr[] = [2, 13]
Output:
-1
Explanation:
No element occurs more than 2/2 times. Hence, no majority element.
🔒 Constraints
$1 \leq \text{arr.size()} \leq 10^5$
$0 \leq \text{arr[i]} \leq 10^5$
✅ My Approach:
Boyer-Moore Voting Algorithm
This algorithm is optimal in both time and space. It works in two phases:
Candidate Selection — Identify a potential majority candidate.
Validation Pass — Count its frequency to confirm majority.
Algorithm Steps:
Initialize
count = 0andcandidate = -1.Traverse the array:
If
count == 0, setcandidate = current element.If current element equals
candidate, incrementcount.Else, decrement
count.
After traversal, validate whether
candidateactually occurs more thann/2times.If yes, return
candidate; else return-1.
🧮 Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), as we traverse the array twice — once to find candidate and once to validate.
Expected Auxiliary Space Complexity: O(1), as we only use a constant number of variables.
💡 Code (C)
🧠 Code (C++)
⚡ Alternative Approaches
📊 2️⃣ Hash Map Frequency Count
Algorithm Steps:
Traverse the array and store the count of each element using a hash map.
Iterate through the map and return the element with frequency > n/2.
📝 Complexity Analysis:
Expected Time Complexity: O(n), as we iterate over the array and map once.
Expected Auxiliary Space Complexity: O(n), due to extra space for frequency map.
📊 3️⃣ Sorting + Middle Element
Algorithm Steps:
Sort the array.
Pick the middle element at index
n/2as the candidate.Count its frequency to validate.
📝 Complexity Analysis:
Expected Time Complexity: O(n log n), due to sorting.
Expected Auxiliary Space Complexity: O(1), assuming in-place sort.
📊 4️⃣ Bit Manipulation
Algorithm Steps:
For each bit (0–31), count the number of times it is set across all elements.
If a bit is set in more than n/2 elements, include it in the result.
Finally, validate the constructed number.
📝 Complexity Analysis:
Expected Time Complexity: O(n), as we check all bits over all elements.
Expected Auxiliary Space Complexity: O(1), using only fixed variables.
🆚 Comparison of Approaches
Approach
⏱️ Time
🗂️ Space
✅ Pros
⚠️ Cons
Boyer-Moore Voting
🟢 O(n)
🟢 O(1)
Optimal time and space
Requires verification pass
Hash Map Frequency
🟢 O(n)
🔴 O(n)
Very easy to code
Extra memory usage
Sorting + Middle Element
🔴 O(n log n)
🟢 O(1)
Simple logic after sort
Sorting overhead
Bit Manipulation
🟢 O(n)
🟢 O(1)
Efficient, constant space
Bit logic slightly tricky
✅ Best Choice?
Scenario
Recommended Approach
✅ Space and speed optimized
🥇 Boyer-Moore Voting
✅ Quick implementation with clarity
🥈 Hash Map Frequency
✅ Sorting already needed or acceptable
🥉 Sorting + Middle Element
✅ Exploring bit-level ideas
🤓 Bit Manipulation
🧑💻 Code (Java)
🐍 Code (Python)
🧠 Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: 📬 Any Questions? — Let’s learn and grow together!
⭐ If this helped you, don't forget to star the repository! ⭐
📍Visitor Count
Last updated