← All solutions

Majority Element

March 03, 2025 • Go •array, hash table, sorting, counting, divide and conquer • easy

Go Solution

func majorityElement(nums []int) int {
    hashMap := make(map[int]int)
   for _,num := range nums {
        hashMap[num]++
        if hashMap[num] > len(nums)/2 {
            return num
        }
   } 
   return 0
}
LeetCode Problem Link