Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

word0 challenge

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
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> 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)
(But I guess compiler optimalisation will do that for you)

Yup, in both cases.