2215. Find the Difference of Two Arrays
Question
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 innums1
which are not present innums2
.answer[1]
is a list of all distinct integers innums2
which are not present innums1
.
Note that the integers in the lists may be returned in any order.
Solution
维持两个Hash Set,将两个数组中的数字加入两个集合。
遍历两个数组,如果另一个数组的集合不含有当前元素,则将元素添加到返回列表中。
Code
1 | class Solution { |
2215. Find the Difference of Two Arrays
https://xuanhe95.github.io/2023/05/03/2215-Find-the-Difference-of-Two-Arrays/