-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmajority_element.rs
More file actions
40 lines (34 loc) · 909 Bytes
/
majority_element.rs
File metadata and controls
40 lines (34 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// # Majority Element
// Given an array nums of size n, return the majority element.
//
// The majority element is the element that appears more than ⌊n / 2⌋ times.
// You may assume that the majority element always exists in the array.
pub fn solution(nums: Vec<i32>) -> i32 {
let mut map = std::collections::HashMap::new();
for num in &nums {
map.entry(num)
.and_modify(|counter| *counter += 1)
.or_insert(1);
}
let majority_hurdle = &nums.len() / 2;
for (num, count) in map {
if count > majority_hurdle {
return *num;
}
}
-1
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn example_1() {
let nums = vec![3, 2, 3];
assert_eq!(solution(nums), 3);
}
#[test]
fn example_2() {
let nums = vec![2, 2, 1, 1, 1, 2, 2];
assert_eq!(solution(nums), 2);
}
}