2215. Find the Difference of Two Arrays LeetCode Solution
In this guide, you will get 2215. Find the Difference of Two Arrays LeetCode Solution with the best time and space complexity. The solution to Find the Difference of Two Arrays problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find the Difference of Two Arrays solution in C++
- Find the Difference of Two Arrays solution in Java
- Find the Difference of Two Arrays solution in Python
- Additional Resources

Problem Statement of Find the Difference of Two Arrays
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
Complexity Analysis
- Time Complexity: O(|\texttt{nums1}| + |\texttt{nums2}|)
- Space Complexity: O(|\texttt{nums1}| + |\texttt{nums2}|)
2215. Find the Difference of Two Arrays LeetCode Solution in C++
class Solution {
public:
vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {
const unordered_set<int> set1{nums1.begin(), nums1.end()};
const unordered_set<int> set2{nums2.begin(), nums2.end()};
vector<vector<int>> ans(2);
for (const int num : set1)
if (!set2.contains(num))
ans[0].push_back(num);
for (const int num : set2)
if (!set1.contains(num))
ans[1].push_back(num);
return ans;
}
};
/* code provided by PROGIEZ */
2215. Find the Difference of Two Arrays LeetCode Solution in Java
class Solution {
public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
Set<Integer> set1 = Arrays.stream(nums1).boxed().collect(Collectors.toSet());
Set<Integer> set2 = Arrays.stream(nums2).boxed().collect(Collectors.toSet());
Arrays.stream(nums1).forEach(set2::remove);
Arrays.stream(nums2).forEach(set1::remove);
return Arrays.asList(new ArrayList<>(set1), new ArrayList<>(set2));
}
}
// code provided by PROGIEZ
2215. Find the Difference of Two Arrays LeetCode Solution in Python
class Solution:
def findDifference(self, nums1: list[int],
nums2: list[int]) -> list[list[int]]:
set1 = set(nums1)
set2 = set(nums2)
return [set1 - set2, set2 - set1]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.