Majority Element-I

Given an integer array nums of size n, return the majority element of the array.

The majority element of an array is an element that appears more than n/2 times in the array. The array is guaranteed to have a majority element.

Example 1:

Input: nums = [7, 0, 0, 1, 7, 7, 2, 7, 7]

Output: 7

Explanation:

The number 7 appears 5 times in the 9 sized array

Example 2:

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

Output: 1

Explanation:

The number 1 appears 4 times in the 6 sized array

Now Your Turn!

Pick the correct output for the given input

Input: nums = [-1, -1, -1, -1]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • n == nums.length.
  • 1 <= n <= 105
  • -104 <= nums[i] <= 104
  • One value appears more than n/2 times.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int majorityElement(vector<int>& nums) {
}
};
Test Case

Input:

Nums