Skip to main content

Remove duplicate lines of a text document

JavaScript version

const removeDuplicateLines = (str) => Array.from(new Set(str.split(/\r?\n/))).join('\n');

TypeScript version

const removeDuplicateLines = (str: string): string => Array.from(new Set(str.split(/\r?\n/))).join('\n');

Examples

removeDuplicateLines(`one
three
two
three
one
four`);

/* Output */
/*
one
three
two
four
*/

Comments