-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 2404: Most Frequent Even Element
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::cmp::Ordering; | ||
use std::collections::hash_map::Entry; | ||
use std::collections::HashMap; | ||
use std::hash::{BuildHasherDefault, DefaultHasher}; | ||
|
||
impl Solution { | ||
pub fn most_frequent_even(nums: Vec<i32>) -> i32 { | ||
let mut frequencies = HashMap::<_, u16, _>::with_hasher(BuildHasherDefault::<DefaultHasher>::default()); | ||
|
||
for num in nums { | ||
if num & 1 == 0 { | ||
match frequencies.entry(num) { | ||
Entry::Occupied(occupied_entry) => *occupied_entry.into_mut() += 1, | ||
Entry::Vacant(vacant_entry) => { | ||
vacant_entry.insert(1); | ||
} | ||
} | ||
} | ||
} | ||
|
||
let mut result = -1; | ||
let mut max_frequency = 0; | ||
|
||
for (&num, &frequency) in &frequencies { | ||
match frequency.cmp(&max_frequency) { | ||
Ordering::Less => {} | ||
Ordering::Equal => { | ||
if num < result { | ||
result = num; | ||
} | ||
} | ||
Ordering::Greater => { | ||
max_frequency = frequency; | ||
result = num; | ||
} | ||
} | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn most_frequent_even(nums: Vec<i32>) -> i32 { | ||
Self::most_frequent_even(nums) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
pub mod hash_map; | ||
|
||
pub trait Solution { | ||
fn most_frequent_even(nums: Vec<i32>) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&[0, 1, 2, 2, 4, 4, 1] as &[_], 2), | ||
(&[4, 4, 4, 9, 2, 4], 4), | ||
(&[29, 47, 21, 41, 13, 37, 25, 7], -1), | ||
( | ||
&[8154, 9139, 8194, 3346, 5450, 9190, 133, 8239, 4606, 8671, 8412, 6290], | ||
3346, | ||
), | ||
]; | ||
|
||
for (nums, expected) in test_cases { | ||
assert_eq!(S::most_frequent_even(nums.to_vec()), expected); | ||
} | ||
} | ||
} |