Solved

word0 challenge

Posted on 2016-09-29
4
64 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 33

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 33

Expert Comment

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

Yup, in both cases.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

911 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

19 Experts available now in Live!

Get 1:1 Help Now