EN
JavaScript - remove duplicated lines
8 points
In this short article, we would like to show how to remove duplicated lines from some text using JavaScript.
Quick solution:
xxxxxxxxxx
1
const newLineExpression = /\r\n|\n\r|\n|\r/g;
2
3
const removeDuplicatedLines = (text) => {
4
let result = '';
5
const blocker = {}; // prevents lines dupplication
6
const lines = text.split(newLineExpression);
7
for (const line of lines) {
8
if (blocker.hasOwnProperty(line)) {
9
continue;
10
}
11
blocker[line] = true;
12
result += line + '\n';
13
}
14
return result;
15
};
16
17
// Usage example:
18
19
const text = `a
20
b
21
b
22
a
23
a
24
c
25
c`;
26
27
console.log(removeDuplicatedLines(text)); // a
28
// b
29
// c
This approach uses a functional programming pattern.
xxxxxxxxxx
1
const newLineExpression = /\r\n|\n\r|\n|\r/g;
2
3
const removeDuplicatedLines = (text) => {
4
return text.split(newLineExpression)
5
.filter((item, index, array) => array.indexOf(item) === index)
6
.join('\n');
7
};
8
9
// Usage example:
10
11
const text = `a
12
b
13
b
14
a
15
a
16
c
17
c`;
18
19
console.log(removeDuplicatedLines(text)); // a
20
// b
21
// c
This approach was created to show that it is possible to get the same effect with reduce()
method.
xxxxxxxxxx
1
const newLineExpression = /\r\n|\n\r|\n|\r/g;
2
3
const removeDuplicatedLines = (text) => {
4
const blocker = {}; // prevents lines dupplication
5
return text.split(newLineExpression)
6
.reduce((result, line) => {
7
if (blocker.hasOwnProperty(line)) {
8
return result;
9
}
10
blocker[line] = true;
11
return result + line + '\n';
12
}, '');
13
};
14
15
// Usage example:
16
17
const text = `a
18
b
19
b
20
a
21
a
22
c
23
c`;
24
25
console.log(removeDuplicatedLines(text)); // a
26
// b
27
// c