diff --git a/src/lib.rs b/src/lib.rs index 5759fbed..610670b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1481,6 +1481,7 @@ pub mod problem_2186_minimum_number_of_steps_to_make_two_strings_anagram_ii; pub mod problem_2190_most_frequent_number_following_key_in_an_array; pub mod problem_2194_cells_in_a_range_on_an_excel_sheet; pub mod problem_2206_divide_array_into_equal_pairs; +pub mod problem_2215_find_the_difference_of_two_arrays; #[cfg(test)] mod test_utilities; diff --git a/src/problem_2215_find_the_difference_of_two_arrays/hash_set.rs b/src/problem_2215_find_the_difference_of_two_arrays/hash_set.rs new file mode 100644 index 00000000..61041764 --- /dev/null +++ b/src/problem_2215_find_the_difference_of_two_arrays/hash_set.rs @@ -0,0 +1,33 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +use std::collections::HashSet; + +impl Solution { + pub fn find_difference(nums1: Vec, nums2: Vec) -> Vec> { + let nums1 = nums1.into_iter().collect::>(); + let nums2 = nums2.into_iter().collect::>(); + + vec![ + nums1.difference(&nums2).copied().collect(), + nums2.difference(&nums1).copied().collect(), + ] + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn find_difference(nums1: Vec, nums2: Vec) -> Vec> { + Self::find_difference(nums1, nums2) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2215_find_the_difference_of_two_arrays/mod.rs b/src/problem_2215_find_the_difference_of_two_arrays/mod.rs new file mode 100644 index 00000000..e77d9515 --- /dev/null +++ b/src/problem_2215_find_the_difference_of_two_arrays/mod.rs @@ -0,0 +1,30 @@ +pub mod hash_set; + +pub trait Solution { + fn find_difference(nums1: Vec, nums2: Vec) -> Vec>; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [ + ( + (&[1, 2, 3] as &[_], &[2, 4, 6] as &[_]), + [&[1, 3] as &[_], &[4, 6] as &[_]], + ), + ((&[1, 2, 3, 3], &[1, 1, 2, 2]), [&[3] as &[_], &[] as &[_]]), + ]; + + for ((nums1, nums2), expected) in test_cases { + let mut result = S::find_difference(nums1.to_vec(), nums2.to_vec()); + + for value in &mut result { + value.sort_unstable(); + } + + assert_eq!(result, expected.map(Vec::from),); + } + } +}