Skip to content

Commit

Permalink
dups program
Browse files Browse the repository at this point in the history
  • Loading branch information
djp424 committed Jan 25, 2017
1 parent bea374b commit 75c4363
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions concepts/2_linked_lists/ctci/1_dups/dups.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.*;

public class dups {
public static void deleteDups( LinkedList ll ) {
HashSet<String> set = new HashSet<String>();

for (int i = 0; i < ll.size(); i = i + 1) {
String currentData = ll.get( i ).toString();
if (set.contains(currentData)) {
ll.remove(i);
System.out.println("Removed Dup: " + currentData);
} else {
set.add(currentData);
}
}

}

public static void main(String args[]) {
LinkedList<String> ll = new LinkedList<String>(); // You should use <String> here

ll.add("J");
ll.add("J");
ll.add("U");
ll.add("S");
ll.add("T");
ll.add("3");
ll.add("I");
ll.add("C");
ll.add("E");
ll.add("3");
System.out.println("Before: " + ll);

deleteDups(ll);

System.out.println("After: " + ll);
}
}

0 comments on commit 75c4363

Please sign in to comment.