Skip to main content

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/2 times.
  • Boyer-Moore Voting Algorithm works in two phases:
    1. Candidate Selection: Iterate and cancel out different elements. If the counter becomes zero, assume the current element as a new candidate.
    2. Verification: Count the candidate’s actual frequency to confirm whether it really occurs more than n/2 times.

🧩 Key Insight / Formula​

  • Boyer-Moore Voting: At most one majority can exist.
  • If an element occurs more than n/2 times, it will always survive the cancellation process.

⏱️ Time & Space Complexity​

MetricValue
⏱ TimeO(n)
πŸ—‚ SpaceO(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​

ApproachTimeSpaceNotes
HashMap (frequency count)O(n)O(n)Simple but uses extra space ❌
Sorting + middle elementO(n log n)O(1)Middle element is majority if valid βœ…
Boyer-Moore (this)O(n)O(1)Best and optimal βœ…


πŸš€ 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.

Discord
Official Hub

Discord Community

Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.

Join Discord Server→
⚑
Daily Knowledge Drops

Daily Visual Deep-Dives

Bite-sized system design schematics, core OS internals, and DSA intuition served right into your feeds every single day.

πŸ’‘Have handwritten notes, cheat-sheets, or want to publish a technical article?
Learn How to Contribute→