diff --git a/rs/intersection-of-two-arrays.rs b/rs/intersection-of-two-arrays.rs new file mode 100644 index 0000000..7b2cc2f --- /dev/null +++ b/rs/intersection-of-two-arrays.rs @@ -0,0 +1,17 @@ +use std::collections::HashSet; + +impl Solution { + pub fn intersection(mut nums1: Vec, mut nums2: Vec) -> Vec { + // if ‹nums1› is bigger, swap them + if nums1.len() > nums2.len() { + return Self::intersection(nums2, nums1); + } + + let nums: HashSet = nums1.into_iter().collect(); + nums2.sort_unstable(); + + nums.into_iter() + .filter(|x| nums2.binary_search(x).is_ok()) + .collect() + } +}