Link to home
Create AccountLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

has22 challenge

Hi,

I am working on below challenge

http://codingbat.com/prob/p121853
I wrote my code as below
public boolean has22(int[] nums) {
 
  
  boolean result=false;

  int sum=0;
  for(int n:nums){
   if(n==2)
   result=true;
  
  }
   return result;




}

Open in new window






I am not passing all tests

Expected      Run            
has22([1, 2, 2]) → true      true      OK      
has22([1, 2, 1, 2]) → false      true      X      
has22([2, 1, 2]) → false      true      X      
has22([2, 2, 1, 2]) → true      true      OK      
has22([1, 3, 2]) → false      true      X      
has22([1, 3, 2, 2]) → true      true      OK      
has22([2, 3, 2, 2]) → true      true      OK      
has22([4, 2, 4, 2, 2, 5]) → true      true      OK      
has22([1, 2]) → false      true      X      
has22([2, 2]) → true      true      OK      
has22([2]) → false      true      X      
has22([]) → false      false      OK      
has22([3, 3, 2, 2]) → true      true      OK      
has22([5, 2, 5, 2]) → false      true      X      
other tests
X      


How to improve my design, approach, code? please advise
ASKER CERTIFIED SOLUTION
Avatar of CPColin
CPColin
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of gudii9

ASKER

let me check
Is this snippet relevant or not in this question:

if(nums[y]==2&&nums[y+1]==2)

Open in new window

Avatar of gudii9

ASKER

public boolean has22(int[] nums) {
 
  
  boolean result=false;

  int sum=0;
  int len=nums.length;
  for(int i=0;i<len-1;i++){
   if(nums[i]==2&&nums[i+1]==2)
   result=true;
  
  }
   return result;




}

Open in new window


above passed all tests? any improvements, suggestions, comments?
You could return true as soon as you find two 2's next to each other and save a little processing time. (If you have an array that's a thousand elements long and the first two elements are 2's, you don't need to check the rest of the array, since you already know you'll be returning true.)
Avatar of gudii9

ASKER

You could return true as soon as you find two 2's next to each other and save a little processing time.

how to do this?
return true;
Avatar of gudii9

ASKER

public boolean has22(int[] nums) {
 
  
 // boolean result=false;

  int sum=0;
  int len=nums.length;
  for(int i=0;i<len-1;i++){
   if(nums[i]==2&&nums[i+1]==2)
  return true;
  
  }
  // return result;
return false;



}

Open in new window


i see . this is very powerful to use return here.
Yep! You can't always break out of a loop early or return from a loop early, but it can be nice, when you can.
i see . this is very powerful to use return here.

I ALREADY posted code with this return clause in it !

Did you even *look* at that ???

You ARE a troll, are you not ????