word0 challenge

Posted on 2016-09-29
Last Modified: 2016-09-30

I am working on below challenge.

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++){
  return map;

Open in new window

i passed all tests. any improvements or alternate approaches? please advise
Question by:gudii9
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
LVL 33

Accepted Solution

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

LVL 37

Assisted Solution

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

LVL 37

Expert Comment

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

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

Expert Comment

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

Yup, in both cases.

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why doesn't this text field show up on my Applet frame? 2 28
Eclipse with various Java releases 7 53
wild fly 8 startup error 2 43
Developing a front end to SPLUNK 1 65
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
In this post we will learn different types of Android Layout and some basics of an Android App.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

730 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