001package algs35;
002import stdlib.*;
003/* ***********************************************************************
004 *  Compilation:  javac DeDup.java
005 *  Execution:    java DeDup < input.txt
006 *  Dependencies: SET StdIn.java StdOut.java
007 *  Data files:   http://algs4.cs.princeton.edu/35applications/tinyTale.txt
008 *
009 *  Read in a list of words from standard input and print out
010 *  each word, removing any duplicates.
011 *
012 *  % more tinyTale.txt
013 *  it was the best of times it was the worst of times
014 *  it was the age of wisdom it was the age of foolishness
015 *  it was the epoch of belief it was the epoch of incredulity
016 *  it was the season of light it was the season of darkness
017 *  it was the spring of hope it was the winter of despair
018 *
019 *  % java DeDup < tinyTale.txt
020 *  it
021 *  was
022 *  the
023 *  best
024 *  of
025 *  times
026 *  worst
027 *  age
028 *  wisdom
029 *  ...
030 *  winter
031 *  despair
032 *
033 *************************************************************************/
034
035public class DeDup {
036        public static void main(String[] args) {
037                StdIn.fromFile ("data/tinyTale.txt");
038
039                SET<String> set = new SET<>();
040
041                // read in strings and add to set
042                while (!StdIn.isEmpty()) {
043                        String key = StdIn.readString();
044                        if (!set.contains(key)) {
045                                set.add(key);
046                                StdOut.println(key);
047                        }
048                }
049        }
050}