-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFindDuplicateCharacter.java
More file actions
37 lines (26 loc) · 897 Bytes
/
FindDuplicateCharacter.java
File metadata and controls
37 lines (26 loc) · 897 Bytes
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
package findDuplicates;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class FindDuplicateCharacter {
public static void main(String[] args) {
String word = "Czechoslovakia";
char[] split = word.toCharArray();
Map<Character, Integer> duplicateCount = new HashMap<Character, Integer>();
for(char character : split) {
if(duplicateCount.containsKey(character)) {
duplicateCount.replace(character, duplicateCount.get(character)+1);
} else {
duplicateCount.put(character, 1);
}
}
// System.out.println(duplicateCount);
for(Entry<Character, Integer> entry: duplicateCount.entrySet()) {
if(entry.getValue()>1) {
System.out.println(entry.getKey()+" have a duplicate with "+entry.getValue()+" duplicate characters");
} else {
System.out.println(entry.getKey()+" doesn't have a duplicate");
}
}
}
}