##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2215.html

# 2215. Find the Difference of Two Arrays (Easy)

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.

Example 1:

Input: nums1 = [1,2,3], nums2 = [2,4,6]
Output: [[1,3],[4,6]]
Explanation:
For nums1, nums1[1] = 2 is present at index 0 of nums2, whereas nums1[0] = 1 and nums1[2] = 3 are not present in nums2. Therefore, answer[0] = [1,3].
For nums2, nums2[0] = 2 is present at index 1 of nums1, whereas nums2[1] = 4 and nums2[2] = 6 are not present in nums2. Therefore, answer[1] = [4,6].

Example 2:

Input: nums1 = [1,2,3,3], nums2 = [1,1,2,2]
Output: [[3],[]]
Explanation:
For nums1, nums1[2] and nums1[3] are not present in nums2. Since nums1[2] == nums1[3], their value is only included once and answer[0] = [3].
Every integer in nums2 is present in nums1. Therefore, answer[1] = [].


Constraints:

• 1 <= nums1.length, nums2.length <= 1000
• -1000 <= nums1[i], nums2[i] <= 1000

Companies:

Related Topics:
Hash Table

Similar Questions:

## Solution 1. Hash Set

• // OJ: https://leetcode.com/problems/find-the-difference-of-two-arrays/
// Time: O(A + B)
// Space: O(A + B)
class Solution {
public:
vector<vector<int>> findDifference(vector<int>& A, vector<int>& B) {
unordered_set<int> sa(begin(A), end(A)), sb(begin(B), end(B));
vector<vector<int>> ans(2);
for (int n : sa) {
if (sb.count(n) == 0) ans[0].push_back(n);
}
for (int n : sb) {
if (sa.count(n) == 0) ans[1].push_back(n);
}
return ans;
}
};

• class Solution:
def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:
s1, s2 = set(nums1), set(nums2)
return [list(s1 - s2), list(s2 - s1)]

############

# 2215. Find the Difference of Two Arrays
# https://leetcode.com/problems/find-the-difference-of-two-arrays/

class Solution:
def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:
s1 = set(nums1)
s2 = set(nums2)

res = [set() for _ in range(2)]

for x in nums1:
if x not in s2:

for x in nums2:
if x not in s1:

return [list(x) for x in res]


• class Solution {
public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
Set<Integer> s1 = convert(nums1);
Set<Integer> s2 = convert(nums2);

List<List<Integer>> ans = new ArrayList<>();
List<Integer> l1 = new ArrayList<>();
List<Integer> l2 = new ArrayList<>();
for (int v : s1) {
if (!s2.contains(v)) {
}
}
for (int v : s2) {
if (!s1.contains(v)) {
}
}
return ans;
}

private Set<Integer> convert(int[] nums) {
Set<Integer> s = new HashSet<>();
for (int v : nums) {
}
return s;
}
}

• func findDifference(nums1 []int, nums2 []int) [][]int {
s1, s2 := make(map[int]bool), make(map[int]bool)
for _, v := range nums1 {
s1[v] = true
}
for _, v := range nums2 {
s2[v] = true
}
ans := make([][]int, 2)
for v := range s1 {
if !s2[v] {
ans[0] = append(ans[0], v)
}
}
for v := range s2 {
if !s1[v] {
ans[1] = append(ans[1], v)
}
}
return ans
}

• function findDifference(nums1: number[], nums2: number[]): number[][] {
return [
[...new Set<number>(nums1.filter(v => !nums2.includes(v)))],
[...new Set<number>(nums2.filter(v => !nums1.includes(v)))],
];
}


• /**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number[][]}
*/
var findDifference = function (nums1, nums2) {
let ans1 = new Set(nums1),
ans2 = new Set(nums2);
for (let num of nums1) {
ans2.delete(num);
}
for (let num of nums2) {
ans1.delete(num);
}
return [Array.from(ans1), Array.from(ans2)];
};


• use std::collections::HashSet;
impl Solution {
pub fn find_difference(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<Vec<i32>> {
vec![
nums1
.iter()
.filter_map(|&v| if nums2.contains(&v) { None } else { Some(v) })
.collect::<HashSet<i32>>()
.into_iter()
.collect(),
nums2
.iter()
.filter_map(|&v| if nums1.contains(&v) { None } else { Some(v) })
.collect::<HashSet<i32>>()
.into_iter()
.collect(),
]
}
}


• class Solution {
/**
* @param Integer[] $nums1 * @param Integer[]$nums2
* @return Integer[][]
*/
function findDifference($nums1,$nums2) {
$rs = [ [] , [] ];$hashtable1 = array_flip(array_unique($nums1));$hashtable2 = array_flip(array_unique($nums2)); for ($m = 0; $m < count($nums1); $m++) { if (!isset($hashtable2[$nums1[$m]])) {
$rs[0][$m] = $nums1[$m];
$hashtable2[$nums1[$m]] = 1; } } for ($n = 0; $n < count($nums2); $n++) { if (!isset($hashtable1[$nums2[$n]])) {
$rs[1][$n] = $nums2[$n];
$hashtable1[$nums2[$n]] = 1; } } return$rs;
}
}


## Discuss

https://leetcode.com/problems/find-the-difference-of-two-arrays/discuss/1886960/