forked from blakeembrey/code-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDuplicatesFromString.java
More file actions
41 lines (37 loc) · 1.23 KB
/
Copy pathRemoveDuplicatesFromString.java
File metadata and controls
41 lines (37 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Program to remove duplicates from a given string
// 1. getUniqueString(String) method uses a boolean array.
// 2. removeDuplicates(String) method uses a hash map.
// Both the methods have O(n) space and time complexity. (n being the string length)
import java.util.HashMap;
public class RemoveDuplicatesFromString {
public static void main(String[] args) {
RemoveDuplicatesFromString rsd = new RemoveDuplicatesFromString();
String input = "Tree Traversal";
System.out.println("Method 1: " +rsd.getUniqueString(input));
System.out.println("Method 2: " +rsd.removeDuplicates(input));
}
public String getUniqueString(String input) {
boolean[] isUsed = new boolean[256];
StringBuffer sb = new StringBuffer();
for (int i = 0; i < input.length(); i++) {
int position = input.charAt(i);
if (!isUsed[position]) {
sb.append(input.charAt(i));
isUsed[position] = true;
}
}
return sb.toString();
}
public String removeDuplicates(String input) {
HashMap<Character, Integer> map = new HashMap<>();
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (!map.containsKey(c)) {
sb.append(c);
map.put(c, 1);
}
}
return sb.toString();
}
}