-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordMultiple.java
More file actions
executable file
·31 lines (26 loc) · 1023 Bytes
/
WordMultiple.java
File metadata and controls
executable file
·31 lines (26 loc) · 1023 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
// Given an array of strings, return a Map<String, Boolean> where each different string is a key and its value is true if that string appears 2 or more times in the array.
//
// wordMultiple(["a", "b", "a", "c", "b"]) → {"a": true, "b": true, "c": false}
// wordMultiple(["c", "b", "a"]) → {"a": false, "b": false, "c": false}
// wordMultiple(["c", "c", "c", "c"]) → {"c": true}
import java.util.Map;
import java.util.*;
public class WordMultiple {
public static Map<String, Boolean> wordMultiple(String[] strings) {
Map<String, Boolean> wordmultiple = new HashMap<>();
for (String word : strings) {
if (wordmultiple.containsKey(word)) {
wordmultiple.put(word, true);
} else {
wordmultiple.put(word, false);
}
}
return wordmultiple;
}
public static void main(String[] args) {
String[] testArray = {"a","b","a","b"};
String[] testArray2 = {"this", "and", "this"};
System.out.println(wordMultiple(testArray));
System.out.println(wordMultiple(testArray2));
}
}