Solved

countEvens challenge

Posted on 2016-08-01
2
74 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB Script to add site to Java Exception List 4 49
egit plugin on eclipse 8 33
Problem to open text file 11 69
couple of eclipse 5 19
This is about my first experience with programming Arduino.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

911 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

22 Experts available now in Live!

Get 1:1 Help Now