EasyArraysAI interview only
Majority Element
Asked atamazongooglemicrosoftappleadobebloomberg
01 · Problem
You are given an integer array nums of length n. Return the majority element: the value that occurs strictly more than n / 2 times (that is, more than floor(n / 2) times).
You may assume a majority element always exists, so the answer is unique.
02 · Examples
Example 01
Input
nums = [3,2,3]
Output
3
3 appears 2 times out of 3, which is more than 3 / 2.
Example 02
Input
nums = [2,2,1,1,1,2,2]
Output
2
2 appears 4 times out of 7, more than half of the array.
Example 03
Input
nums = [6,6,6,7,7]
Output
6
6 appears 3 times out of 5, more than 5 / 2.
03 · Constraints
- 011 <= nums.length <= 5 * 104
- 02-109 <= nums[i] <= 109
- 03A majority element is guaranteed to exist
04 · Optimal complexity
- Time
- O(n)
- Space
- O(1)
05 · Two ways to work on it
Practice it alone or rehearse it as an interview.
Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.