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

180
Views
How to check for duplicates in a list and save each email one by line in a file

I have a list like this:

[
  'test@t-online.de',
  'kontakt@test.de',
  'info@test.de',
  'test@gmx.de',
  'kontakt@test.de',
]

I want to check for duplicates and save the list in a txt file with one email in each line. The final result would be in this case:

      test@t-online.de
      kontakt@test.de
      info@test.de
      test@gmx.de
 

fs.writeFileSync('./results/test.txt', list)

How to do that?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can perform that by performing a loop through the Array if emails and perform a filter to like bellow

let emails = [
  'test@t-online.de',
  'kontakt@test.de',
  'info@test.de',
  'test@gmx.de',
  'kontakt@test.de',
];

let result = emails.reduce((accumulator, current) => {
  if(accumulator.indexOf(current) ===  -1){
    accumulator = accumulator.concat(current);
  }
  return accumulator;
}, []);

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Replace list with [...(new Set(list))].join('\n')

  • new Set() ensures only duplicates are removed and only unique elements remain.
  • join('\n') transforms the Array into a string separated by new-line (\n) character.

const list = [
  'test@t-online.de',
  'kontakt@test.de',
  'info@test.de',
  'test@gmx.de',
  'kontakt@test.de',
];
console.log([...(new Set(list))].join('\n'));

about 4 years ago · Juan Pablo Isaza Report

0

for removing duplicated emails:

arr = [...new Set(arr)];

for writing

var writeStream = fs.createWriteStream('./results/test.txt');
writeStream.on('error', function(e) { 
  /* handel error here */ 
});
arr.forEach(email => writeStream.write(email + '\n'));
writeStream.end();

To append data to an existed file Create write stream in append mode

var writeStream = fs.createWriteStream('./results/test.txt', { 'flags': 'a', 'encoding': null, 'mode': 0666});

refer here

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!