Boyer-Moore Majority Vote Algorithm
Boyer-Moore Majority Vote Algorithm
On this page we will discuss about Boyer–Moore majority vote algorithm in Java. 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 Java.Boyer-Moore Majority Vote Algorithm in Java
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.
Example:
Input: k = 6, arr = [ 1, 2, 5, 2, 2, 2 ] Output: Majority Element is 2
Algorithm :
- Iterate through the array from the beginning to the end.
- 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.
- After the first pass, the candidate is the potential majority element.
- Reset the count to 0.
- Iterate through the array again.
- Count the occurrences of the candidate element.
- If the candidate occurs more than half of the time, it is the majority element.
- If the candidate occurs more than half of the time, return it as the majority element.
- If the candidate occurs less than or equal to half of the time, there is no majority element.
Java Code
import java.util.*; public class Main { public static int majorityElement(int [] nums) { int candidate = 0; int count = 0; for(int num : nums) { if(count == 0) { candidate = num; } count += (num == candidate)? 1 : -1; } count = 0; for(int num : nums) { if(num == candidate) { count++; } } if(count > nums.length/2) { return candidate; } else { return -1; } } public static void main(String[] args) { int [] nums = { 1, 2, 2, 2, 3, 2, 4, 2, 5 }; int majority = majorityElement(nums); if(majority != -1) { System.out.print("The majority element is: " + majority); } else { System.out.print("There is no majority element."); } } }
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
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
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
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- C| C++| Java
- Spiral Order traversal of Tree- C | C++| 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.- C| C++| 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- C | C++ | Java
- Lowest Common Ancestor in Binary Tree- C | C++ | Java
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
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
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- C| C++| Java
- Spiral Order traversal of Tree- C | C++| 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.- C| C++| 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- C | C++ | Java
- Lowest Common Ancestor in Binary Tree. C | C++ | Java
else {
i–;
}
wont it be i–;?