-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapChallenge.java
More file actions
40 lines (35 loc) · 1.37 KB
/
MapChallenge.java
File metadata and controls
40 lines (35 loc) · 1.37 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
import java.util.HashMap;
import java.util.Map;
public class MapChallenge {
//Create a String say "I am sahith, and I am a Software Developer working in John Deere in moline"
//count the number of letters that were repeated and count the number of words that were repeated in a given string.
public static void main(String[] args) {
String str = "i am sahith, and i am a software developer working in john deere in moline";
Map<Character,Integer> charOccurances = new HashMap<>();
char[] chars = str.toCharArray();
System.out.println(chars);
for(char character:chars) {
Integer integer = charOccurances.get(character);
// System.out.println(integer);
if(integer==null){
charOccurances.put(character,1);
}
else{
charOccurances.put(character,integer+1);
}
}
System.out.println(charOccurances);
Map<String,Integer> strOccurances = new HashMap<>();
String[] words=str.split(" ");
System.out.println(words);
for(String word:words){
Integer strInt = strOccurances.get(word);
if(strInt==null){
strOccurances.put(word,1);
}else{
strOccurances.put(word,strInt+1);
}
}
System.out.println(strOccurances);
}
}