Last active
September 30, 2023 06:36
-
-
Save drawers/e0fa2e4bdce8e499534b3a4871bc6f99 to your computer and use it in GitHub Desktop.
Deficient solution to Majority Element
This file contains 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 candidate: Int? = null | |
for (num in nums) { | |
if (count == 0) { | |
candidate = num | |
count++ | |
} else { | |
if (num == candidate) { | |
count++ | |
} else { | |
count-- | |
} | |
} | |
} | |
return candidate!! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment