Mid Coding

Find Majority Element (Boyer-Moore Voting Algorithm) 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