The algorithm relies on a cancellation idea:

Majority vs the Most common

⭐️ (Easy) 169. Majority Element, NeetCode 250

https://leetcode.com/problems/majority-element/description/

Last review: Sep 14, 2025 Next review: I couldn’t recall the essential concept.

Hash Map

func majorityElement(nums []int) int {
	n := len(nums)

	count := make(map[int]int)

	for _, num := range nums {
		count[num]++
		if count[num] > n/2 {
			return num
		}
	}
	return -1
}

Sorting + Sliding Window

func majorityElement(nums []int) int {
	n := len(nums)

	sort.Ints(nums)

	L := 0
	for R := 0; R < n; R++ {
		if nums[R] != nums[L] {
			L = R
		}
		if R-L+1 > n/2 {
			return nums[R]
		}
	}
	return -1
}

Boyer-Moore Voting ⭐️