Back to exercises

Majority Element

Track a running candidate and increment or decrement a counter as you scan. When the counter drops to zero, start fresh with the current value as the new candidate.

Coding Exercises Easy O(n)

Majority Element

Track a running candidate and increment or decrement a counter as you scan. When the counter drops to zero, start fresh with the current value as the new candidate.

Return the element that appears more than n // 2 times in an array.