Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# word0 challenge

Posted on 2016-09-29
Medium Priority
161 Views
Hi,

I am working on below challenge.
http://codingbat.com/prob/p152303

Given an array of strings, return a Map<String, Integer> containing a key for every different string in the array, always with the value 0. For example the string "hello" makes the pair "hello":0. We'll do more complicated counting later, but for this problem the value is simply 0.

word0(["a", "b", "a", "b"]) → {"b": 0, "a": 0}
word0(["a", "b", "a", "c", "b"]) → {"b": 0, "c": 0, "a": 0}
word0(["c", "b", "a"]) → {"b": 0, "c": 0, "a": 0}

0
Question by:gudii9
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 32

Expert Comment

ID: 41822756
Map<String, Integer>
For every unique key (i.e., a string), there is an integer value. In this problem, the integer value is always zero. If the unique key appears more than once in an array, then the map can only have one node for that unique key.

(["a", "b", "a", "c", "b"]) -- this array has three unique keys, "a", "b", "c"
The map therefore can only have three nodes, one for each unique key.
0

LVL 7

Author Comment

ID: 41827031
``````public Map<String, Integer> word0(String[] strings) {
int len=strings.length;
Map<String, Integer> map = new HashMap<String, Integer>();
for(int i=0;i<len;i++){

map.put(strings[i],0);
}
return map;
}
``````
above passes all the tests. any improvements or alternate approaches?
0

LVL 32

Accepted Solution

phoffric earned 2000 total points
ID: 41827313
At work, when code is not indented, we do not even review it. Indentation is that important. In fact, if you learn Python, if  you don't get the indentation exactly right, then you can get very subtle errors. (I think that is overkill in enforcing good coding standards, but now Python is gaining in popularity, so keep that in mind.)

Since you only have a few lines, I was willing to review it. Try to indent in the future. Others have asked you to do that. Don't forget, you can hit the "Preview" button to see how your post with code looks.

Your code is fine, but the code from their Show Solution button is even better:
``````public Map<String, Integer> word0(String[] strings) {
Map<String, Integer> map = new HashMap();
for (String s:strings) {  // no need to compute len as you did since compiler knows the array length
map.put(s, 0);
}
return map;
}
``````

The for-statement says, assign s, a String, to each element in the strings array, one at a time, and use that s in the body of the for-loop. It is just as you did, but much cleaner looking since their solution only has three variables: strings, map and s. It is not just cleaner, but also less error prone, since you have to manipulate you counter variable, i.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question