diff --git a/README.md b/README.md index 155b62c..dc0b361 100644 --- a/README.md +++ b/README.md @@ -127,12 +127,14 @@ Ace Coding Interview with 75 Qs [1732]: ./src/page-16/1732.%20Find%20the%20Highest%20Altitude/largestAltitude.ts [724]: ./src/page-7/724.%20Find%20Pivot%20Index/pivotIndex.ts -| Hash Map / Set | | | -| ---------------------------------------- | -------- | ------ | -| 2215. Find the Difference of Two Arrays | Solution | Easy | -| 1207. Unique Number of Occurrences | Solution | Easy | -| 1657. Determine if Two Strings Are Close | Solution | Medium | -| 2352. Equal Row and Column Pairs | Solution | Medium | +| Hash Map / Set | | | +| ---------------------------------------- | ---------------- | ------ | +| 2215. Find the Difference of Two Arrays | [Solution][2215] | Easy | +| 1207. Unique Number of Occurrences | Solution | Easy | +| 1657. Determine if Two Strings Are Close | Solution | Medium | +| 2352. Equal Row and Column Pairs | Solution | Medium | + +[2215]: ./src/page-21/2215.%20Find%20the%20Difference%20of%20Two%20Arrays/findDifference.ts | Stack | | | | ---------------------------------- | -------- | ------ | diff --git a/src/page-21/2215. Find the Difference of Two Arrays/findDifference.test.ts b/src/page-21/2215. Find the Difference of Two Arrays/findDifference.test.ts new file mode 100644 index 0000000..87aa04d --- /dev/null +++ b/src/page-21/2215. Find the Difference of Two Arrays/findDifference.test.ts @@ -0,0 +1,11 @@ +import { findDifference } from './findDifference'; + +describe('2215. Find the Difference of Two Arrays', () => { + test('findDifference', () => { + expect(findDifference([1, 2, 3], [2, 4, 6])).toStrictEqual([ + [1, 3], + [4, 6], + ]); + expect(findDifference([1, 2, 3, 3], [1, 1, 2, 2])).toStrictEqual([[3], []]); + }); +}); diff --git a/src/page-21/2215. Find the Difference of Two Arrays/findDifference.ts b/src/page-21/2215. Find the Difference of Two Arrays/findDifference.ts new file mode 100644 index 0000000..524b92f --- /dev/null +++ b/src/page-21/2215. Find the Difference of Two Arrays/findDifference.ts @@ -0,0 +1,18 @@ +type FindDifference = (nums1: number[], nums2: number[]) => number[][]; + +/** + * Accepted + */ +export const findDifference: FindDifference = (nums1, nums2) => { + // Create sets from nums1 and nums2 + const set1 = new Set(nums1); + const set2 = new Set(nums2); + + // Find distinct elements in nums1 not in nums2 + const distinctNums1 = Array.from(set1).filter((num) => !set2.has(num)); + + // Find distinct elements in nums2 not in nums1 + const distinctNums2 = Array.from(set2).filter((num) => !set1.has(num)); + + return [distinctNums1, distinctNums2]; +};