hash_table.state
Input
Hash table
Current look-up
Match / window
Stored entry
What is a Majority Element
A majority element appears more than n / 2 times. Count every value in a hash map and return the key whose count crosses that threshold. Hashing makes the count obvious; Boyer–Moore can do it in O(1) space later.
2, 2, 1, 1, 1, 2, 2. Watch counts climb until 2 reaches 4, which is more than 7/2.Interactive trace
Pseudocode & Complexity
| Operation | Time |
|---|---|
| Count + check | O(n) |
| Space | O(n) |
Python implementation