Created
May 7, 2020 05:34
-
-
Save munguial/33296557f333a0b68edb903313711eda to your computer and use it in GitHub Desktop.
Day 6 - Majority Element
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
class Solution { | |
fun majorityElement(nums: IntArray): Int { | |
var count = 0 | |
var winner = 0 | |
nums.forEach { | |
if (count == 0) { | |
winner = it | |
count = 1 | |
} else if (winner == it) { | |
count += 1 | |
} else { | |
count -= 1 | |
} | |
} | |
return winner | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment