Destutter.java
Download Destutter.java
1: import java.util.*;
2:
3: /** Destuddering a list means to remove adjacent duplicates,
4: * for example [1, 4, 2, 2, 3, 3, 5] ==> [1, 4, 2, 3, 5]
5: * (For example, removing extra spaces between words in a document,
6: * removing duplicates from a sorted list (producing a "set"), etc.)
7: * In this case, the list is the command-line arguments (Strings).
8: *
9: * @author Wayne Pollock, Tampa Florida USA
10: */
11:
12: public class Destutter {
13: public static void main ( String [] args ) {
14: String previous = null;
15: List<String> result = new ArrayList<>();
16: for ( String arg : args ) {
17: if ( ! arg.equals( previous ) ) {
18: result.add( arg );
19: previous = arg;
20: }
21: }
22:
23: // Display resulting list with adjacent duplicates removed:
24: // (The argument to result.toArray just tells the JVM the type of the
25: // resulting array; see the Java docs for more info on that.)
26: System.out.println( Arrays.toString( result.toArray(new String[0]) ) );
27: }
28: }