-
Notifications
You must be signed in to change notification settings - Fork 110
/
Occurance.java
35 lines (34 loc) · 869 Bytes
/
Occurance.java
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
/* Find the occurance of each character in a string
* Input: Hello world
* Output:
* H: 1
e: 1
l: 3
o: 2
w: 1
r: 1
d: 1
*/
import java.util.*;
public class Occurance {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine().replace(" ","");
LinkedHashMap<Character,Integer> count = new LinkedHashMap<>();
for (int i = 0; i < s.length(); i++)
{
if(!count.containsKey(s.charAt(i)))
{
count.put(s.charAt(i),1);
}
else
{
count.put(s.charAt(i),count.get(s.charAt(i))+1);
}
}
for(HashMap.Entry<Character,Integer> data : count.entrySet())
{
System.out.println(data.getKey()+": "+data.getValue());
}
}
}