Created
July 2, 2020 07:53
-
-
Save alldroll/6973b83a5e8b83a858bbcb6f7dc944ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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