-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordcount.java
More file actions
40 lines (28 loc) · 818 Bytes
/
Wordcount.java
File metadata and controls
40 lines (28 loc) · 818 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
31
32
33
34
35
36
37
38
39
40
package com.sai;
import java.util.HashMap;
import java.util.Set;
public class Wordcount {
public static void main(String[] args) {
duplicatewordcount dw = new duplicatewordcount();
dw.duplicatecount("This is is a good thing in the is");
}
}
class duplicatewordcount {
public void duplicatecount(String string) {
String[] words = string.split(" ");
HashMap<String, Integer> hashmap = new HashMap<String, Integer>();
for (String word : words) {
if (hashmap.containsKey(word.toLowerCase())) {
hashmap.put(word.toLowerCase(), hashmap.get(word.toLowerCase()) + 1);
} else {
hashmap.put(word.toLowerCase(), 1);
}
}
Set<String> sets = hashmap.keySet();
for (String set : sets) {
if (hashmap.get(set) > 1) {
System.out.println(set + ":" + hashmap.get(set));
}
}
}
}