Solved

word0 challenge

Posted on 2016-09-29
4
77 Views
Last Modified: 2016-09-30
Hi,

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

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;
}

Open in new window


i passed all tests. any improvements or alternate approaches? please advise
0
Comment
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
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 34

Accepted Solution

by:
ste5an earned 250 total points
ID: 41823210
No need for an extra variable:

public Map<String, Integer> word0(String[] strings) {
  Map<String, Integer> result = new HashMap<String, Integer>();
  for (int count = 0; count < strings.length; count++) 
  {
      result.put(strings[count], 0);
  }
  
  return result;
}

Open in new window

0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 250 total points
ID: 41823211
Pretty straightforward.
No real improvements.
You could write your loop also as

for (String s : strings) {
    map.put(s, 0);
}

Open in new window

0
 
LVL 37

Expert Comment

by:zzynx
ID: 41823213
>> No need for an extra variable
I don't know.
Without that variable, for each iteration of the loop you'll evaluate
strings.length
If you store that value in a variable, you only determine the length once.

(But I guess compiler optimalisation will do that for you)
0
 
LVL 34

Expert Comment

by:ste5an
ID: 41823217
(But I guess compiler optimalisation will do that for you)

Yup, in both cases.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Introduction to Processes

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question