The Boyer–Moore Voting Algorithm efficiently finds the majority element in an array—an element that appears more than N/2 times—using two passes. In the first pass, it selects a potential candidate by increasing a counter when the same element is encountered and decreasing it when a different element appears, effectively canceling out non-majority elements. In the second pass, the algorithm verifies whether this candidate actually occurs more than N/2 times. This method runs in O(N) time and requires only O(1) extra space, making it both fast and memory-efficient.
Let us see the algorithm and intuition behind its working, by taking an example -
Input :{1,1,1,1,2,3,5}
Output : 1
Explanation : 1 occurs more than 3 times.
Input : {1,2,3}
Output : -1
Explanation: No element appears more than N/2times.
Intuition Behind Working :
The algorithm is based on the idea that if an element occurs more than N/2 times, then all the remaining elements together must occur less than N/2 times.
While traversing the array, we maintain a candidate and a vote count:
- If the current element matches the candidate, we increment the vote count.
- If it does not match, we decrement the vote count.
- When the vote count becomes
0, it means the current candidate cannot be the majority element, so we select a new candidate.
By the end of the first traversal, the remaining candidate is the potential majority element. A second traversal is required to verify whether it actually appears more than N/2 times.
Steps to implement the algorithm :
Step 1: Find a Candidate
Initialize candidate = -1 and votes = 0.
Traverse the array:
- If
votes == 0, setcandidate = arr[i]andvotes = 1. - If
arr[i] == candidate, incrementvotes. - Otherwise, decrement
votes.
Step 2: Verify the Candidate
- Count the occurrences of the candidate in the array.
- If the count is greater than
N/2, return the candidate. - Otherwise, return
-1.
Dry Run for the Given Example
Given array:arr = {1, 1, 1, 1, 2, 3, 5}
First Traversal (Finding the Candidate)
| Element | 1 | 1 | 1 | 1 | 2 | 3 | 5 |
|---|---|---|---|---|---|---|---|
| Votes | 1 | 2 | 3 | 4 | 3 | 2 | 1 |
| Candidate | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
After the first traversal, the candidate = 1.
Second Traversal (Verifying the Candidate)
| Element | 1 | 1 | 1 | 1 | 2 | 3 | 5 |
|---|---|---|---|---|---|---|---|
| Count | 1 | 2 | 3 | 4 | 4 | 4 | 4 |
Total elements = 7
Majority condition: count > 7 / 2 = 3
Since count = 4 > 3,
1 is the majority element.
// C++ implementation for the above approach
#include <iostream>
using namespace std;
// Function to find majority element
int findMajority(int arr[], int n)
{
int i, candidate = -1, votes = 0;
// Finding majority candidate
for (i = 0; i < n; i++) {
if (votes == 0) {
candidate = arr[i];
votes = 1;
}
else {
if (arr[i] == candidate)
votes++;
else
votes--;
}
}
int count = 0;
// Checking if majority candidate occurs more than n/2
// times
for (i = 0; i < n; i++) {
if (arr[i] == candidate)
count++;
}
if (count > n / 2)
return candidate;
return -1;
}
int main()
{
int arr[] = { 1, 1, 1, 1, 2, 3, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
int majority = findMajority(arr, n);
cout << " The majority element is : " << majority;
return 0;
}
import java.io.*;
class GFG
{
// Function to find majority element
public static int findMajority(int[] nums)
{
int count = 0, candidate = -1;
// Finding majority candidate
for (int index = 0; index < nums.length; index++) {
if (count == 0) {
candidate = nums[index];
count = 1;
}
else {
if (nums[index] == candidate)
count++;
else
count--;
}
}
// Checking if majority candidate occurs more than
// n/2 times
count = 0;
for (int index = 0; index < nums.length; index++) {
if (nums[index] == candidate)
count++;
}
if (count > (nums.length / 2))
return candidate;
return -1;
// The last for loop and the if statement step can
// be skip if a majority element is confirmed to
// be present in an array just return candidate
// in that case
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 1, 1, 1, 2, 3, 5 };
int majority = findMajority(arr);
System.out.println(" The majority element is : "
+ majority);
}
}
# Python implementation for the above approach
# Function to find majority element
def findMajority(arr, n):
candidate = -1
votes = 0
# Finding majority candidate
for i in range (n):
if (votes == 0):
candidate = arr[i]
votes = 1
else:
if (arr[i] == candidate):
votes += 1
else:
votes -= 1
count = 0
# Checking if majority candidate occurs more than n/2
# times
for i in range (n):
if (arr[i] == candidate):
count += 1
if (count > n // 2):
return candidate
else:
return -1
# Driver Code
arr = [ 1, 1, 1, 1, 2, 3, 5 ]
n = len(arr)
majority = findMajority(arr, n)
print(" The majority element is :" ,majority)
using System;
class GFG
{
// Function to find majority element
public static int findMajority(int[] nums)
{
int count = 0, candidate = -1;
// Finding majority candidate
for (int index = 0; index < nums.Length; index++) {
if (count == 0) {
candidate = nums[index];
count = 1;
}
else {
if (nums[index] == candidate)
count++;
else
count--;
}
}
// Checking if majority candidate occurs more than
// n/2 times
count = 0;
for (int index = 0; index < nums.Length; index++) {
if (nums[index] == candidate)
count++;
}
if (count > (nums.Length / 2))
return candidate;
return -1;
// The last for loop and the if statement step can
// be skip if a majority element is confirmed to
// be present in an array just return candidate
// in that case
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 1, 1, 1, 1, 2, 3, 5};
int majority = findMajority(arr);
Console.Write(" The majority element is : "
+ majority);
}
}
// Function to find the majority element
function findMajority(arr) {
let count = 0;
let candidate = -1;
// Phase 1: Find candidate
for (let i = 0; i < arr.length; i++) {
if (count === 0) {
candidate = arr[i];
count = 1;
} else if (arr[i] === candidate) {
count++;
} else {
count--;
}
}
// Phase 2: Verify candidate
count = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === candidate) {
count++;
}
}
return count > arr.length / 2 ? candidate : -1;
}
// Driver code
const arr = [1, 1, 1, 1, 2, 3, 5];
const majority = findMajority(arr);
console.log("The majority element is:", majority);
Output
The majority element is : 1
Time Complexity: O(n) ( For two passes over the array )
Space Complexity: O(1)