Skip to main content
Back to problems
#2215
Easy Algorithms

Find the difference of two arrays

Array Hash Table
81.3% acceptance
Feb 25, 2026
2605
126
Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where: answer[0] is a list of all distinct integers in nums1 which are not present in nums2. answer[1] is a list of all distinct integers in nums2 which are not present in nums1. Note that the integers in the lists may be returned in any order.

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
use std::collections::HashSet;


impl Solution {
  pub fn find_difference(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<Vec<i32>> {
    let s1: HashSet<i32> = nums1.iter().cloned().collect();
    let s2: HashSet<i32> = nums2.iter().cloned().collect();
    vec![
      s1.difference(&s2).cloned().collect(),
      s2.difference(&s1).cloned().collect(),
    ]
  }
}