ExamAdda Logo

Majority Element

Easy

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

The majority element is the element that appears more than ⌊n / 2⌋ times.

You may assume that the majority element always exists in the array.

Example 1

Input

3
3 2 3

Output

3

Explanation

The element 3 appears 2 times, which is more than ⌊3/2⌋ = 1.

Therefore, 3 is the majority element.

Example 2

Input

7
2 2 1 1 1 2 2

Output

2

Explanation

The element 2 appears 4 times, which is more than ⌊7/2⌋ = 3.

Hence, 2 is the majority element.

Constraints

1 ≤ n ≤ 5 × 10⁴
-10⁹ ≤ nums[i] ≤ 10⁹
The majority element always exists.

Hints:

Hint 1

Think about how you can eliminate pairs of different elements while keeping the majority element.

Hint 2
  • The Boyer-Moore Voting Algorithm solves this problem in O(n) time using O(1) extra space.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 1, 2026

Last updated

Aug 1, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output