Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

difference: find the difference between two arrays #50

Open
Tracked by #47
jrTilak opened this issue Apr 26, 2024 · 0 comments
Open
Tracked by #47

difference: find the difference between two arrays #50

jrTilak opened this issue Apr 26, 2024 · 0 comments
Labels
registry Related to registry ie registry, generate function, methods etc

Comments

@jrTilak
Copy link
Owner

jrTilak commented Apr 26, 2024

The difference function to return an array with three elements:

An array of elements that are unique to the first array.
An array of elements that are unique to the second array.
An array of elements that are common to both arrays.

Here's a rough sketch of what the new function might look like:

/**
 * Finds the unique and common elements between two arrays.
 *
 * @template T - The type of elements in the arrays.
 * @param {T[]} arr1 - The first array.
 * @param {T[]} arr2 - The second array.
 * @returns {[T[], T[], T[]]} - An array containing three arrays: 
 *                              the first with elements unique to arr1, 
 *                              the second with elements unique to arr2, 
 *                              and the third with elements common to both.
 */
const difference = <T>(arr1: T[], arr2: T[]): [T[], T[], T[]] => {
  const uniqueToArr1 = arr1.filter(el => !arr2.includes(el));
  const uniqueToArr2 = arr2.filter(el => !arr1.includes(el));
  const commonToBoth = arr1.filter(el => arr2.includes(el));
  return [uniqueToArr1, uniqueToArr2, commonToBoth];
};

This is just simple implementation.

@jrTilak jrTilak mentioned this issue Apr 26, 2024
28 tasks
@jrTilak jrTilak added the registry Related to registry ie registry, generate function, methods etc label Apr 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
registry Related to registry ie registry, generate function, methods etc
Projects
None yet
Development

No branches or pull requests

1 participant