Standard Bar Arrays · Medium O(n) · O(1)
A facilities dashboard aggregates swipe records from one building and knows that a single employee account generated more than half of them. Reporting the busiest account by tallying every swipe would need a records-sized table. Identify the dominant account in one pass with constant extra memory.
Input: An array nums of n integers, one account id per swipe.
Output: The value that occurs more than floor(n / 2) times.
1 <= n <= 5 * 10^4-10^9 <= nums[i] <= 10^9A majority element is guaranteed to existInput: {"nums":[3,2,3]}
Output: 3
Account 3 accounts for two of the three swipes, which exceeds half.
Input: {"nums":[2,2,1,1,1,2,2]}
Output: 2
Account 2 swipes four times out of seven, holding the majority despite the interleaving.