Languages
[Edit]
EN

TypeScript - remove duplicated lines

0 points
Created by:
Diana
720

In this short article, we would like to show how to remove duplicated lines from some text using TypeScript.

Quick solution:

const newLineExpression: RegExp = /\r\n|\n\r|\n|\r/g;

const removeDuplicatedLines = (text: string): string => {
  let result = '';
  const blocker = {}; // prevents lines dupplication
  const lines = text.split(newLineExpression);
  for (const line of lines) {
    if (blocker.hasOwnProperty(line)) {
      continue;
    }
    blocker[line] = true;
    result += line + '\n';
  }
  return result;
};


// Usage example:

const text: string = `a
b
b
a
a
c
c`;

console.log(removeDuplicatedLines(text)); // a
                                          // b
                                          // c

 

filter() based example

This approach uses a functional programming pattern.

const newLineExpression: RegExp = /\r\n|\n\r|\n|\r/g;

const removeDuplicatedLines = (text: string): string => {
  return text
    .split(newLineExpression)
    .filter((item, index, array) => array.indexOf(item) === index)
    .join('\n');
};


// Usage example:

const text: string = `a
b
b
a
a
c
c`;

console.log(removeDuplicatedLines(text)); // a
                                          // b
                                          // c

Output:

a
b
c

reduce() based example

This approach was created to show that it is possible to get the same effect with reduce() method.

const newLineExpression: RegExp = /\r\n|\n\r|\n|\r/g;

const removeDuplicatedLines = (text: string): string => {
  const blocker = {}; // prevents lines dupplication
  return text.split(newLineExpression).reduce((result, line) => {
    if (blocker.hasOwnProperty(line)) {
      return result;
    }
    blocker[line] = true;
    return result + line + '\n';
  }, '');
};


// Usage example:

const text: string = `a
b
b
a
a
c
c`;

console.log(removeDuplicatedLines(text)); // a
                                          // b
                                          // c

Output:

a
b
c
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join