Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

263
Views
Optimal function javascript to eliminate duplicates

So let's say i have a variable that stores a big string and where the
is the separator to the next word. whats the best way to eliminate duplicate words using the
as the indicator.

So imagine we have this:

var notRemoved= " I am col 1 
I am col 2
I am col 11 
I am col 1 
I am col 2

And the result should be like this:

var removed= " I am col 1 
I am col 2
I am col 11 "
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This one has answer here : Get all unique values in a JavaScript array (remove duplicates)

Try this:

function onlyUnique(value, index, self) {
  return self.indexOf(value) === index;
}

// usage example:
var a = ['a', 1, 'a', 2, '1'];
var unique = a.filter(onlyUnique);

console.log(unique); // ['a', 1, 2, '1']
about 4 years ago · Juan Pablo Isaza Report

0

The best way to perfom what you want is by :

  • Splitting your String into an array
  • Removing the duplicates (for example using a Set)
  • Re-creating a string using the join method.

const notRemoved = `I am col 1 
I am col 2
I am col 11 
I am col 1 
I am col 2`



function removeDuplicatesFromString(str){
  const splitArray = notRemoved.split('\n')
  const arrayWithoutDuplicates = [...new Set(splitArray)];

  return arrayWithoutDuplicates.join('\n')
}


console.log(removeDuplicatesFromString(notRemoved))

Note : This can also be done using one line :

let removed = [...new Set(notRemoved.split('\n')].join('\n');

Documentation

split method

Set

join method

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!