🗳️

Majority Element Challenge

Intermediate

Find the majority element in an array using Moore's Voting Algorithm. Learn efficient array algorithms and voting techniques.

15-20 min
Level 1 - Easy

Find Majority Element

Select numbers to analyze and find the element that appears more than n/2 times

Array:

Majority Element - Moore's Voting Algorithm

🎯 Problem Statement

Given an array of integers, find the majority element. The majority element is the element that appears more than ⌊n/2⌋ times. You may assume that the array is non-empty and the majority element always exists in the array.

💡 Key Concepts

Moore's Voting Algorithm

An efficient algorithm that finds the majority element in O(n) time and O(1) space by using a voting mechanism.

Majority Property

If an element appears more than n/2 times, it will remain as the candidate even after canceling out with other elements.

📋 Algorithm Pseudo Code

function findMajorityElement(nums):
    // Initialize candidate and count
    candidate = nums[0]
    count = 1
    
    // First pass: Find potential candidate
    for i from 1 to length(nums) - 1:
        if count == 0:
            // Reset candidate
            candidate = nums[i]
            count = 1
        else if nums[i] == candidate:
            // Increment count for same element
            count += 1
        else:
            // Decrement count for different element
            count -= 1
    
    // Second pass: Verify the candidate
    count = 0
    for i from 0 to length(nums) - 1:
        if nums[i] == candidate:
            count += 1
    
    // Check if candidate is majority
    if count > length(nums) / 2:
        return candidate
    else:
        return null  // No majority element

🔍 Step-by-Step Example

Input: nums = [3, 3, 4, 2, 4, 4, 2, 4, 4]

Step 1: candidate = 3, count = 1

Step 2: nums[1] = 3, count = 2

Step 3: nums[2] = 4, count = 1

Step 4: nums[3] = 2, count = 0, candidate = 2

Step 5: Continue until end, final candidate = 4

Verification: 4 appears 5 times > 9/2 = 4.5 ✓

📊 Voting Visualization

Example: [3, 3, 4, 2, 4, 4, 2, 4, 4]
i=0:candidate=3, count=1
i=1:candidate=3, count=2
i=2:candidate=3, count=1
i=3:candidate=3, count=0
i=4:candidate=2, count=1
Final:candidate=4 ✓

⏱️ Complexity Analysis

Time Complexity

O(n) - Two passes through the array

Space Complexity

O(1) - Only using a few variables

🔄 Alternative Approaches

Hash Map Approach

Count occurrences of each element using a hash map. Time: O(n), Space: O(n)

Sorting Approach

Sort the array and find the middle element. Time: O(n log n), Space: O(1)

Boyer-Moore Voting

The most efficient approach for guaranteed majority element. Time: O(n), Space: O(1)

⚠️ Edge Cases

No Majority Element

When no element appears more than n/2 times, the algorithm may return an incorrect result. Always verify!

Single Element Array

Array with one element is always the majority element.

All Same Elements

When all elements are the same, that element is the majority.

Progress1 / 6
Algorithm:Majority Element
🎯 How to Play

1. Click on numbers in the array to select them

2. Select enough numbers to analyze a potential majority

3. The majority element appears more than n/2 times

4. Use Moore\'s Voting Algorithm for efficiency

5. Complete all levels to win!

📊 Difficulty Levels
Easy: Simple arrays with clear majority
Medium: Complex arrays, no majority cases
Hard: Large arrays, edge cases