-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map use
29 lines (23 loc) · 1.04 KB
/
Map use
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
public class RemoveDuplicatesUsingMap {
public static void main(String[] args) {
String inputString = "Akash Chikhalonde";
// Create a new LinkedHashMap to preserve the order of characters
Map<Character, Integer> charMap = new LinkedHashMap<>();
// Iterate over each character in the string
for (int i = 0; i < inputString.length(); i++) {
char c = inputString.charAt(i);
// If the character is not already in the map, add it with a value of 1
if (!charMap.containsKey(c)) {
charMap.put(c, 1);
}
}
// Use a StringBuilder to build the output string without duplicates
StringBuilder sb = new StringBuilder(charMap.size());
// Iterate over the keys of the map and append them to the StringBuilder
for (Character c : charMap.keySet()) {
sb.append(c);
}
// Print the output string
System.out.println(sb.toString());
}
}