Coders Crushby Napplied AI
Back to DSA Problems
EasyArrays & Hashing

Majority Element

Find element appearing > n/2 times

Solution Approach

Use Boyer-Moore voting algorithm. Track candidate and count, updating when count reaches 0.

Implementation
def majorityElement(nums):
    # Boyer-Moore voting algorithm
    candidate = None
    count = 0
    for num in nums:
        if count == 0:
            candidate = num
        count += 1 if num == candidate else -1
    return candidate
Complexity Analysis

Time Complexity

O(n)

Space Complexity

O(1)
Complexity
Time:O(n)
Space:O(1)
Asked at
FacebookMicrosoftApple
Coders Crushby Napplied AI

The ultimate interview preparation platform. Master System Design, DSA, and tackle community challenges to crush your FAANG interviews.

Looking for jobs? Visit Napplied AI Jobs Search Agent

System Design

  • All Problems
  • Easy
  • Hard

DSA

  • All Problems
  • Dynamic Programming
  • Graphs

More

  • Problems Arena
  • Growth Paths
  • My Crush

Coders Crush by Napplied AI - Tech Interview & Coding Should Be Effortless