-
Notifications
You must be signed in to change notification settings - Fork 0
/
duplicate_words in string
52 lines (45 loc) · 1.3 KB
/
duplicate_words in string
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
import java.util.*;
public class HelloWorld{
public static void main(String []a)
{
printchar("javahaha");
}
public static void printchar(String str){
if(str==null)
{
System.out.println("Null string");
return;
}
if(str.length()==1)
{
System.out.println("single length string");
return;
}
if(str.isEmpty())
{
System.out.println("Empty string");
return;
}
// step 1 is to convert the string into character array
char words[] = str.toCharArray();
//maintain hashmap<key, value>
Map<Character, Integer> charMap = new HashMap<Character, Integer>();
for(Character ch : words )
{
if(charMap.containsKey(ch)){
charMap.put(ch, charMap.get(ch)+1);
}else{
charMap.put(ch,1);
}
}
//print the map
Set<Map.Entry<Character, Integer>> entrySet = charMap.entrySet();
for(Map.Entry<Character,Integer> entry : entrySet)
{
if(entry.getValue()>1)
{
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
}
}