Majority Element
Real-world version: Quorum Drift ValueThe statement here is our own: the same inputs, outputs and technique.
A monitoring service asks every node in a cluster for the same figure: how far that node's clock has drifted from the reference, in signed milliseconds. Nodes with a broken sensor answer with whatever number their hardware happens to produce, so the array reports mixes trustworthy answers with junk, in no particular order.
The cluster is provisioned so that the healthy nodes are always a strict majority: one value occupies more than half of the entries of reports. Return that value.
The array is streamed in from the wire, so solve it with a single left-to-right pass over reports and only a constant amount of bookkeeping. Do not sort and do not tally every distinct value.
Examples
Example 1
- Input
- reports = [4, 7, 4]
- Output
- 4
Two of the three nodes answered 4, which is more than half, so 7 is the faulty reading. Note that the answer is not the entry sitting in the middle.
Example 2
- Input
- reports = [-6, -6, 2, -6, 9]
- Output
- -6
Three entries out of five hold -6. Drift can run either side of the reference, so a negative figure is a perfectly ordinary answer.
Constraints
- n == reports.length
- 1 <= n <= 5 * 10^4
- -10^9 <= reports[i] <= 10^9
- The input is provisioned so that some value appears more than n / 2 times
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def quorum_drift(reports: list[int]) -> int:public int quorumDrift(int[] reports)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 2 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's