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

asked on

countEvens challenge

Hi,

I am working on below challenge

http://codingbat.com/prob/p162010
I wrote my code as below

public int countEvens(int[] nums) {
  int count=0;
  for(int n:nums){
    if(n%2==0){
      count++;
    }
  }
  return count;
  
}

Open in new window


I am passing all tests

How to improve my design, approach, code? please advise
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland 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
Its a shame coding bat doesn't support java 8.
You could have done a one liner something like:

return IntStream.of(nums).filter( i -> i%2 == 0).count();