Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Majority Element

Easy
arrayhash-tabledivide-and-conquer

Given an array nums of size n, return the majority element — the element that appears more than ⌊n/2⌋ times. You may assume it always exists.

Input: one line of space-separated integers. Output: the majority element.

Example 1
Input:
3 2 3
Output:
3
Example 2
Input:
2 2 1 1 1 2 2
Output:
2

Constraints

  • 1 ≤ nums.length ≤ 5·10^4
  • The majority element always exists.
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.