Skip to content

Commit c2e01ee

Browse files
authored
Merge pull request mayankgb2#25 from Nikkoneha/patch-2
count word using hashmap
2 parents adfb805 + 1307b16 commit c2e01ee

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

wordshash.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.HashMap;
2+
3+
public class FinalCountWords {
4+
5+
public static void main(String[] args) {
6+
String str = "This this is is done by Saket Saket";
7+
String[] split = str.split(" ");
8+
9+
HashMap<String,Integer> map = new HashMap<String,Integer>();
10+
for (int i=0; i<split.length; i++) {
11+
if (map.containsKey(split[i])) {
12+
int count = map.get(split[i]);
13+
map.put(split[i], count+1);
14+
}
15+
else {
16+
map.put(split[i], 1);
17+
}
18+
}
19+
System.out.println(map);
20+
}
21+
22+
}

0 commit comments

Comments
 (0)