2215. Find the Difference of Two Arrays

Description

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

Solutions

Solution 1

Python Code
1
2
3
4
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)]

Java Code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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)) {
                l1.add(v);
            }
        }
        for (int v : s2) {
            if (!s1.contains(v)) {
                l2.add(v);
            }
        }
        ans.add(l1);
        ans.add(l2);
        return ans;
    }

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

C++ Code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
public:
    vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int> s1(nums1.begin(), nums1.end());
        unordered_set<int> s2(nums2.begin(), nums2.end());
        vector<vector<int>> ans(2);
        for (int v : s1)
            if (!s2.count(v))
                ans[0].push_back(v);
        for (int v : s2)
            if (!s1.count(v))
                ans[1].push_back(v);
        return ans;
    }
};

Go Code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
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
}

TypeScript Code
1
2
3
4
5
6
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)))],
    ];
}

Rust Code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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()
        ]
    }
}

JavaScript Code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
/**
 * @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)];
};

PHP Code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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;
    }
}

Solution 2

Rust Code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
impl Solution {
    pub fn find_difference(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<Vec<i32>> {
        const N: usize = 2001;
        let to_index = |i| (i as usize) + 1000;

        let mut is_in_nums1 = [false; N];
        let mut is_in_nums2 = [false; N];
        let mut res1 = vec![];
        let mut res2 = vec![];
        for &num in nums1.iter() {
            is_in_nums1[to_index(num)] = true;
        }
        for &num in nums2.iter() {
            is_in_nums2[to_index(num)] = true;
            if !is_in_nums1[to_index(num)] {
                res2.push(num);
                is_in_nums1[to_index(num)] = true;
            }
        }
        for &num in nums1.iter() {
            if !is_in_nums2[to_index(num)] {
                res1.push(num);
                is_in_nums2[to_index(num)] = true;
            }
        }
        vec![res1, res2]
    }
}