-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuplicateCharacter.java
More file actions
54 lines (36 loc) · 1.06 KB
/
DuplicateCharacter.java
File metadata and controls
54 lines (36 loc) · 1.06 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
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.Hashtable;
import java.util.Map.Entry;
class DuplicateCharacter
{
public static void main(String[] args)
{
String str = "This is a string";
String[] arr = str.split(" ");
StringBuilder sb = new StringBuilder();
for(int i = 0 ; i < arr.length ; i++){
sb.append(arr[i]);
}
String send = sb.toString();
printDuplicate(send);
}
public static void printDuplicate(String str){
System.out.println(str);
str = str.toLowerCase();
Hashtable<Character,Integer> hm = new Hashtable<Character,Integer>();
for(int i = 0 ; i < str.length(); i++){
//System.out.println(str.charAt(i));
if(hm.isEmpty()){
hm.put(str.charAt(i),1);
}else if(hm.containsKey(str.charAt(i))){
hm.put(str.charAt(i),hm.get(str.charAt(i)) + 1);
}else{
hm.put(str.charAt(i),1);
}
}
for(Entry<Character,Integer> entry : hm.entrySet()){
if(entry.getValue() > 1){
System.out.println(entry.getKey() +" :: " + entry.getValue());
}
}
}
}