Majority Element
Easy Arrays & Hashing AI round
nums is a list of whole numbers, n of them.
One number in it, the majority, appears more than n / 2 times: more often than all the other numbers put together. Every list given has one.
Return the majority.
Example 1
Input: nums = [4,7,4,4,1]
Output: 4
4 appears 3 times in a list of 5, more than half of it. 7 and 1 appear once each.
Example 2
Input: nums = [-6,-6,0,9,-6,9,-6]
Output: -6
-6 appears 4 times out of 7, and 4 is more than 3.5.
Example 3
Input: nums = [8]
Output: 8
Constraints
1 ≤ n ≤ 2 × 10⁵-10⁹ ≤ nums[i] ≤ 10⁹- One number appears more than
n / 2times
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
Your first AI round is free.
Watch the explanation
More Arrays & Hashing
- Two SumAI round · Easy
- Longest Consecutive SequenceAI round · Medium
- First Missing PositiveHard
- Spiral MatrixMedium
- Set Matrix ZeroesMedium