#349
Easy Algorithms Intersection of two arrays
Array Hash Table Two Pointers Binary Search Sorting
77.5% acceptance
Jan 12, 2026
6864
2344
Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.
Solution
Rust
Time O(2^n)
Space O(n)
impl Solution {
pub fn intersection(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
use std::collections::HashSet;
let set1: HashSet<i32> = nums1.into_iter().collect();
let set2: HashSet<i32> = nums2.into_iter().collect();
set1.intersection(&set2).copied().collect()
}
}