← DiffPush

Majority element

Standard Bar Arrays · Medium O(n) · O(1)

The Dominant Tenant in the Access Records

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.

Constraints

Examples

Example 1

Input: {"nums":[3,2,3]}
Output: 3
Account 3 accounts for two of the three swipes, which exceeds half.

Example 2

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.

Solve this in your browser →

Also on LeetCode ↗