Implementing the Boyer-Moore Voting Algorithm in Swift

Nadia Ahmadian
Nerd For Tech
Published in
3 min readApr 27, 2024

--

In the land of algorithms, the Boyer-Moore Voting Algorithm takes a special place in giving an elegant way for finding the majority element from the array. This algorithm is precise and effective in the sense that it is capable of finding the element that repeats more than half of the time in the list with very low time complexity. Here’s a verbose example of how this algorithm could be implemented in Swift, trying to use as much of its built-in functions and language features as possible.

Overview of the Boyer-Moore Voting Algorithm

The majority element in a list is defined as an element that appears more than half the time. The Boyer-Moore Voting Algorithm effectively finds this element using two passes through the list:

  1. Choosing a Candidate: This involves iterating through the list and using a voting mechanism to potentially identify the majority element.
  2. Verifying the Candidate: The identified candidate from the first pass is then verified in the second pass to ensure it truly is the majority.

This method is efficient due to its linear time complexity, O(n)O(n), and constant space complexity, O(1)O(1).

Swift Implementation

Let’s break down the implementation of this algorithm in Swift:

Step 1: Initialize the Variables

First, we declare a candidate variable to store the potential majority element and a count to track its occurrences.

var candidate: Int?
var count = 0

Step 2: First Pass — Identifying the Candidate

We iterate through the array. If count is zero, we assign the current element as the new candidate. If the current element matches the candidate, we increment the count. Otherwise, we decrement the count.

for num in nums {
if count == 0 {
candidate = num
}
count += (num == candidate) ? 1 : -1
}

Step 3: Second Pass — Verifying the Candidate

Once a candidate has been identified, we make another pass through the array to count its occurrences to confirm it’s the majority element.

if let candidate = candidate {
count = 0
nums.forEach { if $0 == candidate { count += 1 } }

if count > nums.count / 2 {
return candidate
}
}
return nil

Why Is This Approach Efficient?

The Boyer-Moore Voting Algorithm is efficient for several reasons:

  • Single Scan: Most of the work is done in a single scan (two, including verification), which is far more efficient compared to other methods that might require sorting or hash maps.
  • Constant Space: It uses only two variables regardless of the size of the input list, ensuring minimal space usage.
  • Simplicity: The algorithm is straightforward and easy to implement, as demonstrated in the Swift code above.

Conclusion

The Boyer-Moore Voting Algorithm is a powerful tool for situations where you need to find a majority element in an array efficiently. Its implementation in Swift is both concise and effective, making it an excellent choice for iOS developers looking to enhance their algorithmic skills. Whether you’re preparing for coding interviews or just interested in learning more about efficient algorithmic techniques, mastering the Boyer-Moore Voting Algorithm is definitely worthwhile.

The Swift implementation provided is straightforward and can be easily integrated or tested within your iOS projects or playgrounds. This guide should serve as a starting point for those looking to understand and apply this algorithm in a real-world Swift application.

--

--

Nadia Ahmadian
Nerd For Tech

a crazy gemini who believes in magic, aliens and impossible dreams and converts coffee into iOS apps and soon web apps