gudii9
asked on
wordmultiple challenge
Hi,
I am working on below challenge
wordMultiple
do i need to care about the output sorting of values like alphabetical ascending or descending etc.
please advise
I am working on below challenge
wordMultiple
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"]) → {"b": true, "c": false, "a": true}
wordMultiple(["c", "b", "a"]) → {"b": false, "c": false, "a": false}
wordMultiple(["c", "c", "c", "c"]) → {"c": true}
do i need to care about the output sorting of values like alphabetical ascending or descending etc.
please advise
do i need to care about the output sorting of values like alphabetical ascending or descending etc.Does the challenge asks for that? No.
It asks for return a Map. And by nature a Map simply has no key sorting. (a SortedMap has)
ASKER
Here is actual link
http://codingbat.com/prob/p190862
http://codingbat.com/prob/p190862
It asks for return a Map. And by nature a Map simply has no key sorting. (a SortedMap has)only Map not SorterMap here.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
public Map < String, Boolean > wordMultiple(String[] strings) {
Map < String, Boolean > boolMap = new HashMap();
Map < String, Integer > intMap = new HashMap();
for (int i = 0; i < strings.length; i++) {
String buffer = strings[i];
if (intMap.containsKey(buffer)) {
int val = intMap.get(buffer);
val++;
intMap.put(buffer, val);
} else {
intMap.put(buffer, 1);
}
boolMap.put(buffer, intMap.get(buffer) >= 2);
}
return boolMap;
}
above passes all. any improvements or alternate aproaches?
Here's a tiny improvement, IMO:
for (String buffer:strings) {
// String buffer = strings[i];
Nice indenting, BTW.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@zzynx,
In the first half of your last post #a41827513 you say, "that's a replacement for these TWO lines of code:"
But I demonstrated that in the post before you. Why did you repeat this small point?
In the first half of your last post #a41827513 you say, "that's a replacement for these TWO lines of code:"
But I demonstrated that in the post before you. Why did you repeat this small point?
@phoffric
You told that
I corrected that. That's why I wrote TWO with capitals.
You told that
for (String buffer:strings) {
is a replacement for// String buffer = strings[i];
Which is in correct.I corrected that. That's why I wrote TWO with capitals.
Ok. No replacement since a for loop can hardly replace a definition of a variable. Just replacing one for loop with another and then commenting out the duplicate definition of buffer.
Just replacing one for loop with anotherThat wasn't clear to me.
It's always good to use the form
replace
this code
bythat code
{"b": true, "c": false, "a": true}
{"b": false, "c": false, "a": false}
{"c": true}
You will need to iterate thru an array and keep a count of strings. Once counted then you need to check if it is more than two.