Skip to content

Commit

Permalink
perf(difference): Optimize difference (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
po4tion authored Jun 8, 2024
1 parent 795c83b commit 4ec747e
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/array/difference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
* in the second array.
*
* @example
* * const array1 = [1, 2, 3, 4, 5];
* const array1 = [1, 2, 3, 4, 5];
* const array2 = [2, 4];
* const result = difference(array1, array2);
* // result will be [1, 3, 5] since 2 and 4 are in both arrays and are excluded from the result.
*/
export function difference<T>(firstArr: T[], secondArr: T[]): T[] {
return firstArr.filter(item => {
return !secondArr.includes(item);
});
const secondSet = new Set(secondArr);

return firstArr.filter(item => !secondSet.has(item));
}

0 comments on commit 4ec747e

Please sign in to comment.