Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created August 16, 2017 09:16
Show Gist options
  • Save luojiyin1987/d976a5892f2d09b517de85506eca6c26 to your computer and use it in GitHub Desktop.
Save luojiyin1987/d976a5892f2d09b517de85506eca6c26 to your computer and use it in GitHub Desktop.
Majority Element
func majorityElement(nums []int) int {
temp := make(map [int]int)
result :=nums[0]
if len(nums) == 1 {
return result
}
for _, v := range nums {
temp[v]++
}
for k, v := range temp {
if v >len(nums)/2 {
result = k
break
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment