-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp1.java
More file actions
30 lines (25 loc) · 777 Bytes
/
p1.java
File metadata and controls
30 lines (25 loc) · 777 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
/*
* Counting duplicate characters: Write a program that counts duplicate characters
* from a given string.
*/
//new comment-hello world
import java.util.HashMap;
import java.util.Map;
public class p1{
public static void main(String[] args){
String str = "jiggle jiggle";
countchars(str);
}
public static void countchars(String str){
HashMap<Character, Integer> hm = new HashMap<>();
for(int i = 0; i < str.length(); i++){
char c = str.charAt(i);
if(!(hm.containsKey(c)))
hm.put(c, 1);
else
hm.put(c, hm.get(c) + 1);
}
for(Map.Entry en: hm.entrySet())
System.out.println(en.getKey()+" "+en.getValue());
}
}