JavaScript — remove duplicated lines

Hello there! 👋 😊

In this article, I would like to show you two ways to remove duplicated lines from some text in JavaScript. 🔠

Before we start, I would highly recommend you to check out runnable examples for the solution on our website:
JavaScript — remove duplicated lines

We’re gonna use two methods:

  • filter()
  • reduce()

filter() based example

This approach uses a functional programming pattern.

On the text I’ve used set of operations to remove duplicated lines:

  • split() method to split text into lines that take a newline character in 4 different variants which is a universal approach,
  • filter() method that creates new array of elements tested with provided function,
  • join() method to merge items back.

Practical example:

You can run this example here

reduce() based example

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

Practical example:

You can run this example here

If you found this solution useful you can react to this post or just leave a comment to let me know what you think. Thanks for reading and see you next time! 😊

Write to us! ✉

If you have any problem to solve or questions that no one can answer related to a React or JavaScript topic, or you’re looking for a mentoring write to us on dirask.com -> Questions

--

--

--

We share coding tips and tricks in JavaScript ❤️ 💻 🙂. We help to solve JavaScript problems at http://dirask.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

React Interview Questions

What is the difference between Javascript and Typescript?

Google Identity API’s for signing into react applications with google.

How to use Google Analytics and track unique visitors to your portfolio website?

Diving into Open Source Waters

AngularJS Introduction

8 Essential Web Development Tools That You Must Know As A Developer

Vue.js: Routing With Vue-Router

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dirask JavaScript

Dirask JavaScript

We share coding tips and tricks in JavaScript ❤️ 💻 🙂. We help to solve JavaScript problems at http://dirask.com

More from Medium

Why JavaScript Promises are awesome

Angular vs React: Which One to Choose For Your Next Project?

[JavaScript] How to prevent page transition when submitting a form

JavaScript Fetch API Example