diff --git a/cpp/2215-find-the-difference-of-two-arrays.cpp b/cpp/2215-find-the-difference-of-two-arrays.cpp new file mode 100644 index 000000000..83c1bf4c2 --- /dev/null +++ b/cpp/2215-find-the-difference-of-two-arrays.cpp @@ -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> findDifference(vector &nums1, vector &nums2) + { + unordered_set nums1Set(nums1.begin(), nums1.end()); + unordered_set nums2Set(nums2.begin(), nums2.end()); + + vector lst1; + vector 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}; + } +}; \ No newline at end of file diff --git a/javascript/2215-find-the-difference-of-two-arrays.js b/javascript/2215-find-the-difference-of-two-arrays.js new file mode 100644 index 000000000..59bbfac12 --- /dev/null +++ b/javascript/2215-find-the-difference-of-two-arrays.js @@ -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]; +}; diff --git a/python/2215-find-the-difference-of-two-arrays.py b/python/2215-find-the-difference-of-two-arrays.py new file mode 100644 index 000000000..c609e2567 --- /dev/null +++ b/python/2215-find-the-difference-of-two-arrays.py @@ -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] diff --git a/typescript/2215-find-the-difference-of-two-arrays.ts b/typescript/2215-find-the-difference-of-two-arrays.ts new file mode 100644 index 000000000..6251a7327 --- /dev/null +++ b/typescript/2215-find-the-difference-of-two-arrays.ts @@ -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 = new Set(nums1); + const nums2Set: Set = 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]; +}