Solved

word0 challenge

Posted on 2016-09-29
4
52 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
  • 2
  • 2
4 Comments
 
LVL 32

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 32

Expert Comment

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

Yup, in both cases.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now