The integer array nums has n elements, and one value fills more than half of them: it appears more than ⌊n / 2⌋ times. Return that value.
Such a value always exists in the input, and there can only ever be one.
n = 8, so the winner needs more than 4 appearances. 7 has 5.
Three of the five values are -5. 2 appears twice, which is not more than half.
Constraints
n == nums.length1 <= n <= 5 · 10⁴-10⁹ <= nums[i] <= 10⁹⌊n / 2⌋ times.Counting every value is easy. Can you do it in one O(n) pass with only O(1) extra memory? The judge only checks the answer, so that part is up to you.
Cancel pairs of different values against each other. The majority has more copies than every other value put together, so after all the cancelling it is the only value left.
7