Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit a95a285

Browse files
committed
Simple remove dupes imp, is it cheating to use List. Also shame have to it twice to print a nice string
1 parent c12a579 commit a95a285

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
.DS_Store
2+
.idea
3+
/out
4+
/core
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class RemoveDuplicatesFromString {
5+
6+
public static void main(String[] args) {
7+
String input = "tree traversal";
8+
System.out.println("In: " + input);
9+
10+
String result = removeDuplicates(input);
11+
12+
System.out.println("Out: " + result);
13+
}
14+
15+
private static String removeDuplicates(String s) {
16+
char[] chars = s.toCharArray();
17+
List<Character> uniques = new ArrayList<Character>();
18+
for (char c : chars) {
19+
if (uniques.contains(c)) {
20+
continue;
21+
}
22+
uniques.add(c);
23+
}
24+
StringBuilder output = new StringBuilder();
25+
for (char c : uniques) {
26+
output.append(c);
27+
}
28+
return output.toString();
29+
}
30+
31+
}

0 commit comments

Comments
 (0)