Skip to content

Instantly share code, notes, and snippets.

@rjyo
Created September 7, 2012 12:07
Show Gist options
  • Save rjyo/3665639 to your computer and use it in GitHub Desktop.
Save rjyo/3665639 to your computer and use it in GitHub Desktop.
read from a text file and remove all duplicated rows
var fs = require('fs');
function readLines(input, func) {
var remaining = '';
input.on('data', function(data) {
remaining += data;
var index = remaining.indexOf('\n');
while (index > -1) {
var line = remaining.substring(0, index);
remaining = remaining.substring(index + 1);
func(line);
index = remaining.indexOf('\n');
}
});
input.on('end', function() {
if (remaining.length > 0) {
func(remaining);
}
var keys = Object.keys(mailList);
for (var index in keys) {
console.log(keys[index]);
}
});
}
var mailList = {};
function func(data) {
var key = data.toLowerCase().trim();
if (!mailList.hasOwnProperty(key)) {
mailList[key] = 1;
}
}
var input = fs.createReadStream('input.txt');
readLines(input, func);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment