C:\ece595_06\collections>java FindDups2 red green yellow red blue pink blue Unique words: [green, pink, yellow] Duplicate words: [red, blue]
FindDups2.javaimport java.util.*;
public class FindDups2 {
public static void main(String[] args) {
Set<String> uniques = new HashSet<String>();
Set<String> dups = new HashSet<String>();
for (String a : args)
if (!uniques.add(a))
dups.add(a);
// Destructive set-difference
uniques.removeAll(dups);
System.out.println("Unique words: " + uniques);
System.out.println("Duplicate words: " + dups);
}
}
Maintained by John Loomis, updated Tue Oct 15 19:06:46 2013