Skip to content

Instantly share code, notes, and snippets.

@ValentaTomas
Last active November 2, 2022 18:06
Show Gist options
  • Save ValentaTomas/e83e97706ebc97c5f1154e0908949ab9 to your computer and use it in GitHub Desktop.
Save ValentaTomas/e83e97706ebc97c5f1154e0908949ab9 to your computer and use it in GitHub Desktop.
Filter unique or duplicate values from an array
function onlyUnique<T>(value: T, index: number, self: T[]) {
return self.indexOf(value) === index;
}
function onlyNonUnique<T>(value: T, index: number, self: T[]) {
return self.indexOf(value) !== self.lastIndexOf(value);
}
const data = [];
const uniques = data.map(l => l.insertId).filter(onlyUnique);
const duplicates = data.map(l => l.insertId).filter(onlyNonUnique).filter(onlyUnique);
@ValentaTomas
Copy link
Author

This has O(n²) complexity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment