Boyer–Moore Majority Vote Algorithm

Boyer-Moore majority vote algorithm

On this page we will discuss about Boyer–Moore majority vote algorithm in C++ . The Boyer-Moore Majority Vote Algorithm is an efficient algorithm for finding the majority element in an array, which refers to an element that occurs more than half of the time in the array. So lets see this in detail with code in C++ .

Boyer-Moore Majority Vote Algorithm in C_

Boyer-Moore Majority Vote Algorithm in C++

The Boyer-Moore Majority Vote Algorithm is an efficient algorithm for finding the majority element in an array, which is an element that appears more than n/2 times, where n is the length of the array. The algorithm was proposed by Robert S. Boyer and J Strother Moore in 1980 and is widely used in various applications, such as data analysis, voting systems, and image processing.

The Boyer-Moore Majority Vote Algorithm operates in linear time complexity, which makes it efficient for large arrays, and uses constant space. The basic idea of the algorithm is to maintain a counter that starts from zero and iterates through the array, incrementing the counter for each occurrence of a candidate element, and decrementing it for each occurrence of a different element. The algorithm identifies the majority element as the candidate element with the highest count after scanning through the array.

Boyer-Moore Majority Vote Algorithm in C++

Example:

Input: k = 6,  arr = [ 1, 2, 5, 2, 2, 2 ]

Output: Majority Element is 2

Algorithm :

  1. Iterate through the array from the beginning to the end.
  2. For each element in the array:
    • If the count is 0, set the current element as the candidate.
    • If the current element is the same as the candidate, increment the count.
    • If the current element is different from the candidate, decrement the count.
  3. After the first pass, the candidate is the potential majority element.
  4. Reset the count to 0.
  5. Iterate through the array again.
  6. Count the occurrences of the candidate element.
  7. If the candidate occurs more than half of the time, it is the majority element.
  8. If the candidate occurs more than half of the time, return it as the majority element.
  9. If the candidate occurs less than or equal to half of the time, there is no majority element.

C++ Code

Run
#include <iostream>
#include <vector>

using namespace std;

int majorityElement (vector < int >&nums)
{
  int candidate = 0;		// Initialize candidate as the first element
  int count = 0;		// Initialize count as 0

  // First pass: Find the potential majority element
for (int num:nums)
    {
      if (count == 0)
	{
	  candidate = num;	// Set candidate as the current element
	}
      count += (num == candidate) ? 1 : -1;	// Increment or decrement count
    }

  // Second pass: Verify if the candidate is the majority element
  count = 0;			// Reset count
for (int num:nums)
    {
      if (num == candidate)
	{
	  count++;		// Count occurrences of the candidate
	}
    }

  // If the candidate occurs more than half of the time, return it
  if (count > nums.size () / 2)
    {
      return candidate;
    }
  else
    {
      return -1;		// Return -1 if there is no majority element
    }
}

int main ()
{
  // Example usage of the Boyer-Moore Majority Vote Algorithm
  vector < int >nums = { 1, 2, 2, 2, 3, 2, 4, 2, 5 };	// Example input array
  int majority = majorityElement (nums);	// Call the algorithm
  if (majority != -1)
    {
      cout << "The majority element is: " << majority << endl;
    }
  else
    {
      cout << "There is no majority element." << endl;
    }

  return 0;
}

Output

The majority element is: 2

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java