Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Majority Element (easy)
On this page

Problem Statement

Examples

Try it yourself

Problem Statement

Given an array nums having an n elements, identify the element that appears the majority of the time, meaning more than n/2 times.

Examples

  1. Example 1:

    • Input: [1, 2, 2, 3, 2]
    • Expected Output: 2
    • Justification: Here, '2' appears 3 times in a 5-element array, making it the majority element.
  2. Example 2:

    • Input: [4, 4, 4, 4, 7, 4, 4]
    • Expected Output: 4
    • Justification: '4' is the majority element as it appears 5 out of 7 times.
  3. Example 3:

    • Input: [9, 9, 1, 1, 9, 1, 9, 9]
    • Expected Output: 9
    • Justification: '9' is the majority element, appearing 5 times in an 8-element array.

Constraints:

  • n == nums.length
  • 1 <= n <= 5 * 10<sup>4</sup>
  • -10<sup>9</sup> <= nums[i] <= 10<sup>9</sup>

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

On this page

Problem Statement

Examples

Try it yourself