-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUncommonWords.java
More file actions
55 lines (46 loc) · 1.16 KB
/
UncommonWords.java
File metadata and controls
55 lines (46 loc) · 1.16 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package HashTable;
import java.util.HashMap;
import java.util.Map;
/**
* Author - archit.s
* Date - 20/08/18
* Time - 11:20 AM
*/
public class UncommonWords {
public String[] uncommonFromSentences(String A, String B) {
Map<String, Integer> map = new HashMap<>();
String[] s1 = A.split(" ");
String[] s2 = B.split(" ");
for(int i=0; i<s1.length; i++){
if(map.containsKey(s1[i])){
map.put(s1[i], map.get(s1[i])+1);
}
else{
map.put(s1[i], 1);
}
}
for(int i=0; i<s2.length; i++){
if(map.containsKey(s2[i])){
map.put(s2[i], map.get(s2[i])+1);
}
else{
map.put(s2[i], 1);
}
}
int count = 0;
for(String s: map.keySet()){
if(map.get(s) == 1){
count++;
}
}
String[] s = new String[count];
int index = 0;
for(String temp: map.keySet()){
if(map.get(temp) == 1){
s[index] = temp;
index++;
}
}
return s;
}
}