Mid From PDF Coding C# Coding Interview

Find Majority Element (Boyer-Moore Voting?

lgorithm)

int MajorityElement(int[] nums)
{
int count = 0, candidate = 0;
foreach (var num in nums)
{
if (count == 0)
candidate = num;
count += (num == candidate) ? 1 : -1;
}
return candidate;
}

Explanation:

Maintain a candidate and count; majority element survives this cancellation.

More from C# Programming Tutorial

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details