Skip to content

Instantly share code, notes, and snippets.

@orrisroot
Created October 10, 2025 12:20
Show Gist options
  • Select an option

  • Save orrisroot/d5ef2e077aa4982a072c7a9ff75475e5 to your computer and use it in GitHub Desktop.

Select an option

Save orrisroot/d5ef2e077aa4982a072c7a9ff75475e5 to your computer and use it in GitHub Desktop.
arrayUnique - Removes duplicate items from an array based on a comparison function.
/**
* arrayUnique - Removes duplicate items from an array based on a comparison function.
*
* @param items The array to filter.
* @param compare A function to compare two items for equality.
* @returns A new array with duplicate items removed.
*
* Usage example:
* const uniqueItems = arrayUnique<Item>(items, (a, b) => a.id === b.id);
*/
const arrayUnique = <T>(items: T[], compare: (a: T, b: T) => boolean = (a, b) => a === b): T[] =>
items.filter((item, index, self) => index === self.findIndex((v) => compare(v, item)));
export { arrayUnique };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment