Skip to main content
Back to problems
Leetcode
Medium
Arrays
Hash Maps
Math
Amazon
Google
Microsoft
Majority Element

Find the element that appears more than half the time in an array.

Acceptance 100%
Problem Statement

Problem

Given an integer array nums, find the majority element — the value that appears more than ⌊n / 2⌋ times, where n is the length of the array.

You may assume that a majority element always exists.

Your task is to return that element.

Notes

  • The majority element is guaranteed to be unique.
  • You should aim for an efficient solution in both time and extra space.

Input Format

  • A single integer array nums.
  • Each value in nums is an integer.

Output Format

  • Return the integer that appears more than ⌊n / 2⌋ times.

Constraints

  • 1 <= nums.length
  • A majority element exists.
  • Values may be negative, zero, or positive.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [3,2,3]

Output

3

Explanation

The value 3 appears twice in an array of length 3, which is more than ⌊3/2⌋ = 1.

Example 2

Input

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

Output

2

Explanation

The value 2 appears 4 times in an array of length 7, which is more than ⌊7/2⌋ = 3.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.