Solved

countEvens challenge

Posted on 2016-08-01
2
58 Views
Last Modified: 2016-08-02
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
0
Comment
Question by:gudii9
2 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 41738279
You can't improve it
0
 
LVL 15

Expert Comment

by:gurpsbassi
ID: 41738973
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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
A short article about problems I had with the new location API and permissions in Marshmallow
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

757 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

20 Experts available now in Live!

Get 1:1 Help Now