id 1398669238 Time: 3 ms MemUsage: 2.4 MB

This commit is contained in:
2024-09-23 00:33:01 +09:00
parent 49c9c6a4d6
commit d95bd32b9e

View File

@@ -0,0 +1,18 @@
// BoyerMoore majority vote algorithm
impl Solution {
pub fn majority_element(nums: Vec<i32>) -> i32 {
let (mut maj, mut count) = (0, 0);
for &x in &nums{
if count == 0 {
maj = x;
count = 1;
} else if maj == x {
count += 1;
} else {
count -= 1;
}
}
maj
}
}