Find the Majority Element that occurs more than N/2 times
Problem Statement:β
Β Given an array ofΒ N integers, write a program to return an element that occurs more thanΒ N/2Β times in the given array. You may consider that such an element always exists in the array.
Example 1:
Input Format: N = 3, nums[] = {3,2,3}
Result: 3
Explanation: When we just count the occurrences of each number and compare with half of the size of the array, you will get 3 for the above solution.
Example 2:
Input Format: N = 7, nums[] = {2,2,1,1,1,2,2}
Result: 2
Explanation: After counting the number of times each element appears and comparing it with half of array size, we get 2 as result.
Example 3:
Input Format: N = 10, nums[] = {4,4,2,4,3,4,4,3,2,4}
β Solution: Boyer-Moore Voting Algorithmβ
class Solution {
public:
int majorityElement(vector<int>& array) {
int count = 0;
int candidate = -1;
int size = array.size();
// Phase 1: Find a potential majority candidate
for (int element : array) {
if (count == 0) {
candidate = element; // Set new candidate
}
if (element == candidate)
count++; // Confirm the candidate
else
count--; // Disagree vote
}
// Phase 2: Confirm if candidate is truly the majority
count = 0;
for (int element : array) {
if (element == candidate)
count++;
}
// If frequency of candidate > n/2, return it; else return -1
return count > size / 2 ? candidate : -1;
}
};
π How It Worksβ
- The problem asks us to find the majority element, i.e., the element that appears more than
n/2times. - Boyer-Moore Voting Algorithm works in two phases:
- Candidate Selection: Iterate and cancel out different elements. If the counter becomes zero, assume the current element as a new candidate.
- Verification: Count the candidateβs actual frequency to confirm whether it really occurs more than
n/2times.
π§© Key Insight / Formulaβ
- Boyer-Moore Voting: At most one majority can exist.
- If an element occurs more than
n/2times, it will always survive the cancellation process.
β±οΈ Time & Space Complexityβ
| Metric | Value |
|---|---|
| β± Time | O(n) |
| π Space | O(1) |
β οΈ Edge Casesβ
- All elements same β returns that element β
- No element appears > n/2 β returns -1 β
- Exactly n/2 elements same β returns -1 (since not strictly more than half) β
- Array of size 1 β returns that one element β
π‘ Other Approachesβ
| Approach | Time | Space | Notes |
|---|---|---|---|
| HashMap (frequency count) | O(n) | O(n) | Simple but uses extra space β |
| Sorting + middle element | O(n log n) | O(1) | Middle element is majority if valid β |
| Boyer-Moore (this) | O(n) | O(1) | Best and optimal β |
π Related Problemsβ
- 169. Majority Element
- 229. Majority Element II (more than n/3 times)
- Mooreβs Voting Algorithm
π Level Up Your Engineering Mindset
Never Study in Isolation. Join Binary Dose.
Connect with fellow engineers, clear conceptual doubts, and get 2 visual breakdowns & engineering mental models daily across our community channels.
Official Hub
Discord Community
Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.