Skip to content

Commit 09b56b0

Browse files
authored
Merge pull request #2691 from AHTHneeuhl/2215
2 parents b6b4f0f + c192ee1 commit 09b56b0

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Time Complexity: O(n), where n is the maximum length between nums1 and nums2.
2+
// Space Complexity: O(m), where m is the length of the resulting difference vectors.
3+
4+
class Solution
5+
{
6+
public:
7+
vector<vector<int>> findDifference(vector<int> &nums1, vector<int> &nums2)
8+
{
9+
unordered_set<int> nums1Set(nums1.begin(), nums1.end());
10+
unordered_set<int> nums2Set(nums2.begin(), nums2.end());
11+
12+
vector<int> lst1;
13+
vector<int> lst2;
14+
15+
for (const auto &num : nums1Set)
16+
{
17+
if (nums2Set.find(num) == nums2Set.end())
18+
{
19+
lst1.push_back(num);
20+
}
21+
}
22+
23+
for (const auto &num : nums2Set)
24+
{
25+
if (nums1Set.find(num) == nums1Set.end())
26+
{
27+
lst2.push_back(num);
28+
}
29+
}
30+
31+
return {lst1, lst2};
32+
}
33+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums1
3+
* @param {number[]} nums2
4+
* @return {number[][]}
5+
*/
6+
7+
// Time Complexity: O(n), where n is the maximum length between nums1 and nums2.
8+
// Space Complexity: O(m), where m is the length of the resulting difference lists.
9+
10+
var findDifference = function (nums1, nums2) {
11+
const nums1Set = new Set(nums1);
12+
const nums2Set = new Set(nums2);
13+
14+
const lst1 = Array.from(nums1Set).filter((num) => !nums2Set.has(num));
15+
const lst2 = Array.from(nums2Set).filter((num) => !nums1Set.has(num));
16+
17+
return [lst1, lst2];
18+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Time Complexity: O(n), where n is the maximum length between nums1 and nums2.
2+
# Space Complexity: O(m), where m is the length of the resulting difference lists.
3+
4+
from typing import List # ignore this, just for typing
5+
6+
7+
class Solution:
8+
def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:
9+
nums1_set = set(nums1)
10+
nums2_set = set(nums2)
11+
lst1 = [num for num in nums1_set if num not in nums2_set]
12+
lst2 = [num for num in nums2_set if num not in nums1_set]
13+
return [lst1, lst2]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Time Complexity: O(n), where n is the maximum length between nums1 and nums2.
2+
// Space Complexity: O(m), where m is the length of the resulting difference lists.
3+
4+
function findDifference(nums1: number[], nums2: number[]): number[][] {
5+
const nums1Set: Set<number> = new Set(nums1);
6+
const nums2Set: Set<number> = new Set(nums2);
7+
8+
const lst1: number[] = Array.from(nums1Set).filter(
9+
(num: number) => !nums2Set.has(num)
10+
);
11+
const lst2: number[] = Array.from(nums2Set).filter(
12+
(num: number) => !nums1Set.has(num)
13+
);
14+
15+
return [lst1, lst2];
16+
}

0 commit comments

Comments
 (0)