-
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 2215: Find the Difference of Two Arrays
- Loading branch information
Showing
3 changed files
with
64 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
33 changes: 33 additions & 0 deletions
33
src/problem_2215_find_the_difference_of_two_arrays/hash_set.rs
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,33 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::collections::HashSet; | ||
|
||
impl Solution { | ||
pub fn find_difference(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<Vec<i32>> { | ||
let nums1 = nums1.into_iter().collect::<HashSet<_>>(); | ||
let nums2 = nums2.into_iter().collect::<HashSet<_>>(); | ||
|
||
vec![ | ||
nums1.difference(&nums2).copied().collect(), | ||
nums2.difference(&nums1).copied().collect(), | ||
] | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn find_difference(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<Vec<i32>> { | ||
Self::find_difference(nums1, nums2) | ||
} | ||
} | ||
|
||
#[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,30 @@ | ||
pub mod hash_set; | ||
|
||
pub trait Solution { | ||
fn find_difference(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<Vec<i32>>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
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),); | ||
} | ||
} | ||
} |