Majority Element

Problem Summary

(Write in your own words, not copied from LeetCode. This forces comprehension.)

Key Observations

(Patterns, constraints, or hints in the problem statement.)

Approach Taken

(Step-by-step logic or pseudocode before coding.)

Why This Works

(Explain the core reason the solution is correct.)

Main Concepts Used

(Mark the CS concepts or algorithms used.)

Time & Space Complexity

Code

class Solution:
    def majorityElement(self, nums: List[int]) -> int:
        count = 0
        candidate = None

        for num in nums:
            if count == 0: # find new candidate
                candidate = num
                count += 1  
            elif candidate == num:
                count += 1
            else: # cancel out the count
                count -= 1
        return candidate

Common Mistakes / Things I Got Stuck On

if count == 0: # find new candidate
	candidate = num
	count += 1 # THIS HERE or count = 1