-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0199484
commit 23ab594
Showing
3 changed files
with
37 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/page-21/2215. Find the Difference of Two Arrays/findDifference.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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], []]); | ||
}); | ||
}); |
18 changes: 18 additions & 0 deletions
18
src/page-21/2215. Find the Difference of Two Arrays/findDifference.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]; | ||
}; |