Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2691 from AHTHneeuhl/2215
Browse files Browse the repository at this point in the history
  • Loading branch information
aakhtar3 authored Jul 12, 2023
2 parents b6b4f0f + c192ee1 commit 09b56b0
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
33 changes: 33 additions & 0 deletions cpp/2215-find-the-difference-of-two-arrays.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Time Complexity: O(n), where n is the maximum length between nums1 and nums2.
// Space Complexity: O(m), where m is the length of the resulting difference vectors.

class Solution
{
public:
vector<vector<int>> findDifference(vector<int> &nums1, vector<int> &nums2)
{
unordered_set<int> nums1Set(nums1.begin(), nums1.end());
unordered_set<int> nums2Set(nums2.begin(), nums2.end());

vector<int> lst1;
vector<int> lst2;

for (const auto &num : nums1Set)
{
if (nums2Set.find(num) == nums2Set.end())
{
lst1.push_back(num);
}
}

for (const auto &num : nums2Set)
{
if (nums1Set.find(num) == nums1Set.end())
{
lst2.push_back(num);
}
}

return {lst1, lst2};
}
};
18 changes: 18 additions & 0 deletions javascript/2215-find-the-difference-of-two-arrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number[][]}
*/

// Time Complexity: O(n), where n is the maximum length between nums1 and nums2.
// Space Complexity: O(m), where m is the length of the resulting difference lists.

var findDifference = function (nums1, nums2) {
const nums1Set = new Set(nums1);
const nums2Set = new Set(nums2);

const lst1 = Array.from(nums1Set).filter((num) => !nums2Set.has(num));
const lst2 = Array.from(nums2Set).filter((num) => !nums1Set.has(num));

return [lst1, lst2];
};
13 changes: 13 additions & 0 deletions python/2215-find-the-difference-of-two-arrays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Time Complexity: O(n), where n is the maximum length between nums1 and nums2.
# Space Complexity: O(m), where m is the length of the resulting difference lists.

from typing import List # ignore this, just for typing


class Solution:
def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:
nums1_set = set(nums1)
nums2_set = set(nums2)
lst1 = [num for num in nums1_set if num not in nums2_set]
lst2 = [num for num in nums2_set if num not in nums1_set]
return [lst1, lst2]
16 changes: 16 additions & 0 deletions typescript/2215-find-the-difference-of-two-arrays.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Time Complexity: O(n), where n is the maximum length between nums1 and nums2.
// Space Complexity: O(m), where m is the length of the resulting difference lists.

function findDifference(nums1: number[], nums2: number[]): number[][] {
const nums1Set: Set<number> = new Set(nums1);
const nums2Set: Set<number> = new Set(nums2);

const lst1: number[] = Array.from(nums1Set).filter(
(num: number) => !nums2Set.has(num)
);
const lst2: number[] = Array.from(nums2Set).filter(
(num: number) => !nums1Set.has(num)
);

return [lst1, lst2];
}

0 comments on commit 09b56b0

Please sign in to comment.