Back to DSA sheet

Majority Element

EasyArrays
Open on LeetCodeAmazonAppleGoogle

Given an array of size n, return the element that appears more than n/2 times. You may assume such a majority element always exists.

Input format (stdin): the first line has n. The second line has n integers. Output the majority element.

Examples
Input: 3 3 2 3
Output: 3
3 appears twice out of three elements.
Input: 7 2 2 1 1 1 2 2
Output: 2
2 appears four times, which is more than 7/2.
Constraints
  • 1 <= n <= 5 * 10^4
  • A majority element (count > n/2) is guaranteed to exist
Sheets
Grind 75NeetCode 150NeetCode 250Striver A2Z
majority-element.cpp3 sample tests
Loading editor
Test results

Run the sample tests to check your solution against expected output.

Custom input (stdin)
Output

Run your code to see its output.