Skip to content

Instantly share code, notes, and snippets.

@ssuravarapu
Created July 22, 2011 03:10
Show Gist options
  • Save ssuravarapu/1098814 to your computer and use it in GitHub Desktop.
Save ssuravarapu/1098814 to your computer and use it in GitHub Desktop.
Duplicate Contacts - Java and Scala
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
public class DuplicateWords {
public static void main(String[] args) {
try{
FileReader fileReader = new FileReader(args[0]);
BufferedReader br = new BufferedReader(fileReader);
FileWriter outFile = new FileWriter(args[1]);
PrintWriter out = new PrintWriter(outFile);
removeDuplicates(br, out);
fileReader.close();
out.close();
} catch (Exception e) {
System.err.println("Error: " + e);
}
}
public static void removeDuplicates(BufferedReader br, PrintWriter out)
throws IOException {
String strLine;
Set<String> contactSet = new TreeSet<String>();
while ((strLine = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(strLine, ";");
while(st.hasMoreTokens()) {
String name = st.nextToken().trim();
contactSet.add(name);
out.println(name + "; ");
}
}
}
}
import io.Source
import java.io.FileWriter
object DupWords {
def main(args: Array[String]) {
val names = Source.fromFile(args(0)).mkString.split(";").toList.map(_.trim)
val fw = new FileWriter(args(1))
fw.write(names.distinct.mkString(";" + System.getProperty("line.separator")))
fw.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment