Majority Element

Given an array of size n, return the element that appears more than floor(n/2) times. Sorting moves all equal values together, making the majority easy to identify.

Input Format

nums = array of integers

Output Format

majority element

Constraints

  • 1 <= nums.length <= 5 * 10^4; -10^9 <= nums[i] <= 10^9

Examples

Example 1:

Input:

nums = [3,2,3]

Output:

3

Explanation:

3 appears twice, which is more than floor(3/2).

Example 2:

Input:

nums = [2,2,1,1,1,2,2]

Output:

2

Explanation:

2 appears four times, which is more than floor(7/2).

Loading...
Majority Element - Sorting Based Array Problems