Skip to content

Instantly share code, notes, and snippets.

@drawers
Last active September 30, 2023 06:36
Show Gist options
  • Save drawers/e0fa2e4bdce8e499534b3a4871bc6f99 to your computer and use it in GitHub Desktop.
Save drawers/e0fa2e4bdce8e499534b3a4871bc6f99 to your computer and use it in GitHub Desktop.
Deficient solution to Majority Element
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