Skip to content

Instantly share code, notes, and snippets.

@alldroll
Created July 2, 2020 07:53
Show Gist options
  • Select an option

  • Save alldroll/6973b83a5e8b83a858bbcb6f7dc944ac to your computer and use it in GitHub Desktop.

Select an option

Save alldroll/6973b83a5e8b83a858bbcb6f7dc944ac to your computer and use it in GitHub Desktop.
const undefined = (1 << 31) - 1
func majorityElement(nums []int) int {
candidate := undefined
count := 0
for _, num := range nums {
if count == 0 {
candidate = num
}
if candidate == num {
count++
} else {
count--
}
}
return candidate
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment