Skip to the code
cf.
+10 XP

#68 Verify a True Majority

easy

A feature flag rolls out only if one variant number in nums was chosen by more than half of the users. Nothing is guaranteed this time.

Write trueMajority(nums) that returns the variant chosen more than nums.length / 2 times, or -1 if there is none. Use O(1) extra memory.

Example 1

Input: nums = [2,2,1,2,3]
Output: 2

2 appears 3 times out of 5.

Example 2

Input: nums = [1,2,3,1]
Output: -1

1 appears 2 times out of 4, which is exactly half, not more.

Constraints

  • 0 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).