Last active
November 2, 2022 18:06
-
-
Save ValentaTomas/e83e97706ebc97c5f1154e0908949ab9 to your computer and use it in GitHub Desktop.
Filter unique or duplicate values from an array
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
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This has O(n²) complexity