We need to find the element that appears more than ⌊n / 2⌋ times in the array. The problem guarantees such an element exists, so we don't need to verify our answer, only find it.
The "more than n/2 times" condition has a useful consequence. The majority element occupies more than half the array, so no other element can also appear more than n/2 times. The majority element is therefore unique. This property is what makes the linear-time, constant-space algorithm later in this chapter possible.
Count how many times each element appears, then return the one whose count exceeds n/2. A hash map gives O(1) lookups and updates for the counting.
We iterate through the array once, incrementing the count for each element. We can check the count right after each increment and return early the moment an element crosses the threshold, rather than counting everything first and scanning the map afterward.
n / 2, return that element immediately.This approach is correct but uses O(n) extra space. The next approach removes the hash map by exploiting the fact that the majority element appears more often than all others combined.
If you sort the array, the majority element always ends up at index n/2.
After sorting, equal values form one contiguous block. The majority element's block is longer than n/2 elements, so it must contain the middle index. If the majority element is the smallest value, its block runs from index 0 through at least n/2. If it is the largest, its block runs from at most n/2 to n-1. Either way, index n/2 falls inside the block.
So we sort and read the middle element. No counting needed.
n / 2.Sorting costs O(n log n) time, yet we only read one element afterward. The next approach finds the majority element in a single linear pass with O(1) space by using the "more than half" property to cancel out non-majority elements.
Walk through the array maintaining a "candidate" value and a counter. When the current element equals the candidate, increment the counter. When it differs, decrement the counter. When the counter reaches zero, the next element becomes the new candidate with a counter of 1. The candidate left at the end is the majority element.
A decrement pairs one occurrence of the candidate with one occurrence of a different element and discards both. Each such pair removes one majority and one non-majority element at most, so the difference (majority count minus non-majority count) never decreases below its true value. If the majority element appears m times with m > n/2, then the other n - m elements can cancel at most n - m of its occurrences. Since m > n - m, at least one occurrence survives every cancellation, so the majority element is the candidate when the pass ends.
candidate to any value and count to 0.count is 0, set the current element as the new candidate and set count to 1.candidate, increment count.count.candidate.Whenever the counter resets to zero, the processed prefix contains an equal number of candidate and non-candidate elements. Discarding that prefix leaves the rest of the array, and the majority element of the full array remains the majority of what is left, because removing equal counts of two values cannot flip which value is in the majority. The algorithm effectively peels off these balanced prefixes until only the true majority element remains.
candidate and count, and no additional data structures.