DSA SectionEasy

Boyer Moore Voting

Detailed guide and Python implementation for the 'Boyer Moore Voting' problem.

Problem Statement

Easy

Write a function boyer_moore_majority(arr) that implements the Boyer-Moore Majority Vote Algorithm to find the majority element (the element that appears more than n // 2 times) in a list of integers arr. If no majority element exists, return -1.

Constraints
  • 1 <= len(arr) <= 5 * 10^4
  • -10^9 <= arr[i] <= 10^9

Examples

Example 1
Input
arr = [1, 1, 1, 2, 2]
Output
1
Explanation

1 appears 3 times which is greater than 5 // 2 = 2 times.

Example 2
Input
arr = [1, 2, 3]
Output
-1
Explanation

No element appears more than 3 // 2 = 1 time.

Need a Hint?
Consider using Arrays-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.