350. Intersection of Two Arrays II

Given two arrays, write a function to compute their intersection.

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]

Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]

Note:

  • Each element in the result should appear as many times as it shows in both arrays.
  • The result can be in any order.

Follow up:

  • What if the given array is already sorted? How would you optimize your algorithm?
  • What if nums1's size is small compared to nums2's size? Which algorithm is better?
  • What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?

Rust Solution

struct Solution;

use std::collections::HashMap;
use std::collections::HashSet;

impl Solution {
    fn intersect(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
        let mut hm1: HashMap<i32, i32> = HashMap::new();
        let hs1: HashSet<i32> = nums1.clone().into_iter().collect();
        nums1.iter().for_each(|&x| {
            *hm1.entry(x).or_default() += 1;
        });
        let mut hm2: HashMap<i32, i32> = HashMap::new();
        let hs2: HashSet<i32> = nums2.clone().into_iter().collect();
        nums2.iter().for_each(|&x| {
            *hm2.entry(x).or_default() += 1;
        });
        let bitand = &hs1 & &hs2;
        let mut res: Vec<i32> = vec![];
        bitand.iter().for_each(|&x| {
            let c1 = hm1[&x];
            let c2 = hm2[&x];
            let min = i32::min(c1, c2);
            (0..min).for_each(|_| {
                res.push(x);
            });
        });
        res
    }
}

#[test]
fn test() {
    let nums1 = vec![1, 2, 2, 1];
    let nums2 = vec![2, 2];
    assert_eq!(Solution::intersect(nums1, nums2), vec![2, 2]);
}

Having problems with this solution? Click here to submit an issue on github.