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
LVL 7
gudii9Asked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
You can't improve it
0
 
gurpsbassiCommented:
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();
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.