1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-19 17:56:55 +02:00
LeetCode/rs/intersection-of-two-arrays.rs
Matej Focko dbd0883d68
rs: add «349. Intersection of Two Arrays»
Signed-off-by: Matej Focko <mfocko@redhat.com>
2024-03-10 22:23:40 +01:00

17 lines
484 B
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use std::collections::HashSet;
impl Solution {
pub fn intersection(mut nums1: Vec<i32>, mut nums2: Vec<i32>) -> Vec<i32> {
// if nums1 is bigger, swap them
if nums1.len() > nums2.len() {
return Self::intersection(nums2, nums1);
}
let nums: HashSet<i32> = nums1.into_iter().collect();
nums2.sort_unstable();
nums.into_iter()
.filter(|x| nums2.binary_search(x).is_ok())
.collect()
}
}