lastDigit challenge

Hi,

I am working on below challenge
http://codingbat.com/prob/p169213

I tried my code as below

public boolean lastDigit(int a, int b, int c) {

if( ((a%10==2)==(b%10==2))&&((b%10==2)==(c%10==2)) )
return true;

else 
return false;
  
}

Open in new window



I am getting below result



Expected      Run            
lastDigit(23, 19, 13) → true      true      OK         
lastDigit(23, 19, 12) → false      false      OK         
lastDigit(23, 19, 3) → true      true      OK         
lastDigit(23, 19, 39) → true      true      OK         
lastDigit(1, 2, 3) → false      false      OK         
lastDigit(1, 1, 2) → true      false      X         
lastDigit(1, 2, 2) → true      false      X         
lastDigit(14, 25, 43) → false      true      X         
lastDigit(14, 25, 45) → true      true      OK         
lastDigit(248, 106, 1002) → false      false      OK         
lastDigit(248, 106, 1008) → true      true      OK         
lastDigit(10, 11, 20) → true      true      OK         
lastDigit(0, 11, 0) → true      true      OK         
other tests
X         


how to  improve my approach and design of this challenge. How do i make a

graphical venn or some other relevant diagram to design it before writing single

line of code to decide best strategy?
 Please advise
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

krakatoaCommented:
return a%10==b%10||a%10==c%10||b%10==c%10;

Open in new window

0
krakatoaCommented:
There are 3 possible pair combinations in the three params. You only have to compare the members of each pair, after having removed any powers of 10 they hold, by applying the mod operator % to get rid of everything but the units.
0
ozoCommented:
public boolean lastDigit(int a, int b, int c) {
  a%=10; b%=10; c%=10;
  return a==b||b==c||c==a;
}

public boolean lastDigit(int a, int b, int c) {
  return (a-b)%10==0||(b-c)%10==0||(c-a)%10==0;
}

public boolean lastDigit(int a, int b, int c) {
  return (a-b)*(b-c)*(c-a)%10==0;    // could fail if it overflows Integer.MAX_VALUE
}

public boolean lastDigit(int a, int b, int c) {
  return (a-b)%10*((b-c)%10)*((c-a)%10)==0;  // avoids overflow
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

krakatoaCommented:
public boolean lastDigit(int a, int b, int c) {

  String[] sA = {Integer.toString(a),Integer.toString(b),Integer.toString(c)};
  int q=0;
      if(sA[q].substring(sA[q].length()-1).equals(sA[q+1].substring(sA[q+1].length()-1))||sA[q].substring(sA[q].length()-1).equals(sA[q+2].substring(sA[q+2].length()-1))||sA[q+1].substring(sA[q+1].length()-1).equals(sA[q+2].substring(sA[q+2].length()-1))){return true;}
  
  return false;
  
}

Open in new window


:)

Is there anyone out there  . . . . ?
0
krakatoaCommented:
public boolean lastDigit(int a, int b, int c) {
  
  Set<String> al = new HashSet<String>();
  
  al.add(Integer.toString(a).substring(Integer.toString(a).length()-1));
  al.add(Integer.toString(b).substring(Integer.toString(b).length()-1));;
  al.add(Integer.toString(c).substring(Integer.toString(c).length()-1));
  
  return al.size()<=2;
  
}

Open in new window


:)
0
gudii9Author Commented:
i will re run and let you know.
0
ozoCommented:
public boolean lastDigit(int a, int b, int c) {
    return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");
}
0
gudii9Author Commented:
return a%10==b%10||a%10==c%10||b%10==c%10;

above i understood other i am still trying to understand
0
gudii9Author Commented:
return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");
difference then % sum should be 0?

i am not clear. please advise
0
krakatoaCommented:
" contains("0") "

the "0" would be there - in the String - if any of the 3 pairs last digits are the same (after using % to remove their multiples of 10 (if any). So the "0" means there must have been a pair (at least) with 2 same last digits.
0
krakatoaCommented:
This makes it easier to understand:

return (""+(a-b)+(b-c)+(c-a)%10).contains("0");

Open in new window

0
ozoCommented:
string + number is string concatenation after converting number to string
Is it easy to understand that  (""+(a-b)+(b-c)+(c-a)%10).contains("0") would fail if one of the tests was
lastDigit(1,102,3)
0
krakatoaCommented:
Which two (or more) final digits are the same in " 1, 102, and 3" ??
0
ozoCommented:
none

But penultimate digits are the same
0
krakatoaCommented:
right. You are right.
0
krakatoaCommented:
Again, shows up codingbat's shortcomings at least in terms of their "invisible" "other tests". Misleading results.
0
gudii9Author Commented:
public boolean lastDigit(int a, int b, int c) {
  
  Set<String> al = new HashSet<String>();
  
  al.add(Integer.toString(a).substring(Integer.toString(a).length()-1));
  al.add(Integer.toString(b).substring(Integer.toString(b).length()-1));;
  al.add(Integer.toString(c).substring(Integer.toString(c).length()-1));
  
  return al.size()<=2;
  
}

Open in new window



i was not clear on above?
you are Stringifying a, b,c and doing substring to find last digit an d then adding to al and checking sized less than or equal to 2?

why only 2 why not 3? i was not clear?
please advise
0
gudii9Author Commented:
string + number is string concatenation after converting number to string

above is clear.
Is it easy to understand that  (""+(a-b)+(b-c)+(c-a)%10).contains("0") would fail if one of the tests was
lastDigit(1,102,3)

above not clear. Why it will fail for 1, 10,23
a-b is 1-102=-101?//no right as it became string cannot perform subtract??but it is in between parenthesis ()??
0
ozoCommented:
lastDigit(1,102,3)
not
1, 10,23
0
krakatoaCommented:
I'm not sure I can add anything more to this question. In fact, I am sure - I cannot add anything more.
0
ozoCommented:
why only 2 why not 3?
If al.size()==3, it would mean that
Integer.toString(a).substring(Integer.toString(a).length()-1), Integer.toString(b).substring(Integer.toString(b).length()-1), and Integer.toString(c).substring(Integer.toString(c).length()-1)
were all different.
0
gudii9Author Commented:
lastDigit(1,102,3)
not
1, 10,23

sorry i mean lastDigit(1,102,3)

i did not understand what you mean by penultimate and why this fail?

public class Test10 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		lastDigit(1,102,3);
	}
	public static boolean lastDigit(int a, int b, int c) {
		System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0"));
	return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");
	}
}

Open in new window


i got output
is--->false
0
gudii9Author Commented:
public class Test10 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		lastDigit(8,0,9);
	}
	public static boolean lastDigit(int a, int b, int c) {
		System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10));
		System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0"));
	return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");
	}
}

Open in new window


above gave below
is--->8-91
is--->false
0
gudii9Author Commented:
why only 2 why not 3?
If al.size()==3, it would mean that
Integer.toString(a).substring(Integer.toString(a).length()-1), Integer.toString(b).substring(Integer.toString(b).length()-1), and Integer.toString(c).substring(Integer.toString(c).length()-1)
were all different.

this is clear

import java.util.HashSet;
import java.util.Set;


public class Test10 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//lastDigit(0,0,9);
		lastDigit(1,11,3);
	}
	public static boolean lastDigit(int a, int b, int c) {
		//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10));
		//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0"));
	//return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");
		
		//public boolean lastDigit(int a, int b, int c) {
			  
			  Set<String> al = new HashSet<String>();
			  
			  al.add(Integer.toString(a).substring(Integer.toString(a).length()-1));
			  al.add(Integer.toString(b).substring(Integer.toString(b).length()-1));
			  al.add(Integer.toString(c).substring(Integer.toString(c).length()-1));
			  System.out.println("al is "+al.toString());
			  return al.size()<=2;
			  
			}
	}

Open in new window


i got
al is [1, 3]
0
gudii9Author Commented:
import java.util.HashSet;
import java.util.Set;


public class Test10 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//lastDigit(0,0,9);
		lastDigit(1,11,3);
	}
	public static boolean lastDigit(int a, int b, int c) {
		//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10));
		//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0"));
	//return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");
		
		//public boolean lastDigit(int a, int b, int c) {
			  
			  Set<String> al = new HashSet<String>();
			  
			  al.add(Integer.toString(a).substring(Integer.toString(a).length()-1));
			  al.add(Integer.toString(b).substring(Integer.toString(b).length()-1));
			  al.add(Integer.toString(c).substring(Integer.toString(c).length()-1));
			 // System.out.println("al is "+al.toString());
			  return al.size()<=2;
			  
			}
	}

Open in new window

i wonder above not resulting any boolean value as output?
0
gudii9Author Commented:
we are saying atmost 2 not more than 2
0
ozoCommented:
i got output
is--->false
So do you see why (""+(a-b)%10+(b-c)%10+(c-a)%10)) works better than (""+(a-b)+(b-c)+(c-a)%10)?
0
gudii9Author Commented:
to summarize we have 7 approaches for this challenge


solution 1:

public boolean lastDigit(int a, int b, int c) {
 
  Set<String> al = new HashSet<String>();
 
  al.add(Integer.toString(a).substring(Integer.toString(a).length()-1));
  al.add(Integer.toString(b).substring(Integer.toString(b).length()-1));;
  al.add(Integer.toString(c).substring(Integer.toString(c).length()-1));
 
  return al.size()<=2;
 
}




solution 2:


public boolean lastDigit(int a, int b, int c) {
    return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");
}





solution 3


public boolean lastDigit(int a, int b, int c) {
  a%=10; b%=10; c%=10;
  return a==b||b==c||c==a;
}

solution 4
public boolean lastDigit(int a, int b, int c) {
  return (a-b)%10==0||(b-c)%10==0||(c-a)%10==0;
}

solution 5
public boolean lastDigit(int a, int b, int c) {
  return (a-b)*(b-c)*(c-a)%10==0;    // could fail if it overflows Integer.MAX_VALUE
}

solution6

public boolean lastDigit(int a, int b, int c) {
  return (a-b)%10*((b-c)%10)*((c-a)%10)==0;  // avoids overflow
}

solution 7
return a%10==b%10||a%10==c%10||b%10==c%10;





So do you see why (""+(a-b)%10+(b-c)%10+(c-a)%10)) works better than (""+(a-b)+(b-c)+(c-a)%10)?



 (""+(a-b)%10+(b-c)%10+(c-a)%10))
above means find difference then do % to find last digit on each then add right?
 (""+(a-b)+(b-c)+(c-a)%10)?

above means find difference then add to get sum then finally find the last digit on the sum which is not better approach right as it fails for lastDigit(1,102,3) due to penultimate digit??
i am not still clear on this
0
gudii9Author Commented:
import java.util.HashSet;
import java.util.Set;


public class CopyOfTest10 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//lastDigit(0,0,9);
		System.out.println("valiue is--->"+lastDigit(1,11,3));
	}
	public static boolean lastDigit(int a, int b, int c) {
		//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10));
		//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0"));
	//return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");
		
		//public boolean lastDigit(int a, int b, int c) {
			  
			  Set<String> al = new HashSet<String>();
			  
			  al.add(Integer.toString(a).substring(Integer.toString(a).length()-1));
			  al.add(Integer.toString(b).substring(Integer.toString(b).length()-1));
			  al.add(Integer.toString(c).substring(Integer.toString(c).length()-1));
			  System.out.println("al is "+al.toString());
			  return (al.size()<=2);
			  
			}
	}

Open in new window

output is
al is [1, 3]
valiue is--->true

i got my answer why boolean not printed as i never printed in calling method. i corrected as above
0
gudii9Author Commented:
import java.util.HashSet;
import java.util.Set;

public class CopyOfTest10 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// lastDigit(0,0,9);
		System.out.println("valiue is--->" + lastDigit(1, 102, 3));
	}

	public static boolean lastDigit(int a, int b, int c) {
		System.out.println("is--->"
				+ ("" + (a - b) % 10 + (b - c) % 10 + (c - a) % 10));
		System.out.println("is--->"
				+ ("" + (a - b) % 10 + (b - c) % 10 + (c - a) % 10)
						.contains("0"));
		return ("" + (a - b) % 10 + (b - c) % 10 + (c - a) % 10).contains("0");

		// System.out.println (""+(a-b)+(b-c)+(c-a)%10);
		// System.out.println("is--->"+
		// (""+(a-b)+(b-c)+(c-a)%10).contains("0"));
		// return (""+(a-b)+(b-c)+(c-a)%10).contains("0");

	

		

	}
}

Open in new window


above gave below output
is--->-192
is--->false
valiue is--->false


import java.util.HashSet;
import java.util.Set;

public class CopyOfTest10 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// lastDigit(0,0,9);
		System.out.println("valiue is--->" + lastDigit(1, 102, 3));
	}

	public static boolean lastDigit(int a, int b, int c) {
		// System.out.println("is--->"
		// + ("" + (a - b) % 10 + (b - c) % 10 + (c - a) % 10));
		// System.out.println("is--->"
		// + ("" + (a - b) % 10 + (b - c) % 10 + (c - a) % 10)
		// .contains("0"));
		// return ("" + (a - b) % 10 + (b - c) % 10 + (c - a) %
		// 10).contains("0");

		System.out.println("" + (a - b) + (b - c) + (c - a) % 10);
		System.out.println("is--->"
				+ ("" + (a - b) + (b - c) + (c - a) % 10).contains("0"));
		return ("" + (a - b) + (b - c) + (c - a) % 10).contains("0");

	}
}

Open in new window


above gave output as

-101992
is--->true
valiue is--->true


which is wrong it should have given false.
0
gudii9Author Commented:
So do you see why (""+(a-b)%10+(b-c)%10+(c-a)%10)) works better than (""+(a-b)+(b-c)+(c-a)%10)?


now i see why it works better.

better to make individual differences then reminder by deviding with 10 and contactinate and check contains 0

" contains("0") "

the "0" would be there - in the String - if any of the 3 pairs last digits are the same (after using % to remove their multiples of 10 (if any). So the "0" means there must have been a pair (at least) with 2 same last digits.


as -192 do not have 0 so false whereas
-101992 has 0 so true ideally should give false
0
gudii9Author Commented:
Integer.toString(a).substring(Integer.toString(a).length()-1)

what is meaning of above line.

we are calling toString of wrapper class of int a and doing substring to print last digit??not clear here??

import java.util.HashSet;
import java.util.Set;


public class Test10 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//lastDigit(0,0,9);
		System.out.println("value is---->"+lastDigit(1,11,3));
	}
	public static boolean lastDigit(int a, int b, int c) {
		//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10));
		//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0"));
	//return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");
		
		//public boolean lastDigit(int a, int b, int c) {
			  
			  Set<String> al = new HashSet<String>();
			  System.out.println("al 1 is--->"+Integer.toString(a).substring(Integer.toString(a).length()-1));
			  System.out.println("al 2 is--->"+Integer.toString(b).substring(Integer.toString(a).length()-1));
			  System.out.println("al 3 is--->"+Integer.toString(c).substring(Integer.toString(a).length()-1));
			  
			  al.add(Integer.toString(a).substring(Integer.toString(a).length()-1));
			  al.add(Integer.toString(b).substring(Integer.toString(b).length()-1));
			  al.add(Integer.toString(c).substring(Integer.toString(c).length()-1));
			  System.out.println("al is "+al.toString());
			  return (al.size()<=2);
			  
			}
	}

Open in new window


above cocd gave below output
al 1 is--->1
al 2 is--->11
al 3 is--->3
al is [1, 3]
value is---->true


why al has only 1, 3 but not 11. please advise
0
gudii9Author Commented:

solution 5
public boolean lastDigit(int a, int b, int c) {
  return (a-b)*(b-c)*(c-a)%10==0;    // could fail if it overflows Integer.MAX_VALUE
}

solution6

public boolean lastDigit(int a, int b, int c) {
  return (a-b)%10*((b-c)%10)*((c-a)%10)==0;  // avoids overflow
}

wht it means by overflow???
0
ozoCommented:
System.out.println("al 2 is--->"+Integer.toString(b).substring(Integer.toString(a).length()-1));
System.out.println("al 3 is--->"+Integer.toString(c).substring(Integer.toString(a).length()-1))
should be
System.out.println("al 2 is--->"+Integer.toString(b).substring(Integer.toString(b).length()-1));
System.out.println("al 3 is--->"+Integer.toString(c).substring(Integer.toString(c).length()-1))
0
ozoCommented:
public boolean lastDigit(int a, int b, int c) {
  return (a-b)*(b-c)*(c-a)%10==0;    // could fail if it overflows Integer.MAX_VALUE
}
2000000000000 does not fit in Integer.MAX_VALUE
so lastDigit(10000,20000,30000) would not work properly
0
gudii9Author Commented:
package snippet;
import java.util.HashSet;
import java.util.Set;


public class Test10 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//lastDigit(0,0,9);
		System.out.println("value is---->"+lastDigit(1,11,3));
	}
	public static boolean lastDigit(int a, int b, int c) {
		//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10));
		//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0"));
	//return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");
		
		//public boolean lastDigit(int a, int b, int c) {
			  
			  Set<String> al = new HashSet<String>();
			  System.out.println("al 1 is--->"+Integer.toString(a).substring(Integer.toString(a).length()-1));
			  System.out.println("al 2 is--->"+Integer.toString(b).substring(Integer.toString(b).length()-1));
			  System.out.println("al 3 is--->"+Integer.toString(c).substring(Integer.toString(c).length()-1));
			  
			  al.add(Integer.toString(a).substring(Integer.toString(a).length()-1));
			  al.add(Integer.toString(b).substring(Integer.toString(b).length()-1));
			  al.add(Integer.toString(c).substring(Integer.toString(c).length()-1));
			  System.out.println("al is "+al.toString());
			  return (al.size()<=2);
			  
			}
	}

Open in new window


i fixed as you mentioned and got below output


al 1 is--->1
al 2 is--->1
al 3 is--->3
al is [1, 3]
value is---->true


i wonder why i got al is [1, 3] but not as al is [1,11, 3] which is being passed? please advise
0
gudii9Author Commented:
Integer.toString(a).substring(Integer.toString(a).length()-1)

Open in new window


what is the complete meaning of above line.
please advise
0
ozoCommented:
Integer.toString(b).substring(Integer.toString(b).length()-1)  is the last digit of b
0
gudii9Author Commented:
Integer.toString(b).substring(Integer.toString(b).length()-1)  is the last digit of b

to get last digit of b why are we doing all this round about?
Integer.toString(b).substring(Integer.toString(b).length()-1)
what above line means , can you please break it down and explain in simple terms
0
ozoCommented:
krakatoa might have thought you could learn something from it.
I thought that challenge verbose, when a simple %10 is sufficient.
0
gudii9Author Commented:
Integer.toString(b).substring(Integer.toString(b).length()-1)

where should i put more ( ) so that it is better readable and understandable and breakable?
0
ozoCommented:
bString=Integer.toString(b);
bLength=bString.length();
bLast=bLength-1;
bString.substring(bLast);

If you lookup the java documentation, you should be able to break apart any expression yourself.
0
gudii9Author Commented:
bString.substring(bLast);

how above gives last digit


http://www.tutorialspoint.com/java/java_string_substring.htm


String Str = new String("Welcome to Tutorialspoint.com");

      System.out.print("Return Value :" );
      System.out.println(Str.substring(10) );

when i pass startIndex 10 i get below output right?

Return Value : Tutorialspoint.com
0
ozoCommented:
So what output do you think you would get from
  System.out.println(Str.substring(Str.length()-1) );
0
gudii9Author Commented:
if string Str is abc

then Str.length()-1 ===3-1=2

(Str.substring(Str.length()-1) ); is

(Str.substring(2) );

we get c
i think i got it now.

thank you
0
gudii9Author Commented:
i wonder why i got al is [1, 3] but not as al is [1,11, 3] which is being passed? please advise

package snippet;
import java.util.HashSet;
import java.util.Set;


public class Test10 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//lastDigit(0,0,9);
		System.out.println("value is---->"+lastDigit(1,11,3));
	}
	public static boolean lastDigit(int a, int b, int c) {
		//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10));
		//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0"));
	//return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");
		
		//public boolean lastDigit(int a, int b, int c) {
			  
			  Set<String> al = new HashSet<String>();
			  System.out.println("al 1 is--->"+Integer.toString(a).substring(Integer.toString(a).length()-1));
			  System.out.println("al 2 is--->"+Integer.toString(b).substring(Integer.toString(b).length()-1));
			  System.out.println("al 3 is--->"+Integer.toString(c).substring(Integer.toString(c).length()-1));
			  
			  al.add(Integer.toString(a).substring(Integer.toString(a).length()-1));
			  al.add(Integer.toString(b).substring(Integer.toString(b).length()-1));
			  al.add(Integer.toString(c).substring(Integer.toString(c).length()-1));
			  System.out.println("al is "+al.toString());
			  return (al.size()<=2);
			  
			}
	}

Open in new window


al 1 is--->1
al 2 is--->1
al 3 is--->3
al is [1, 3]
value is---->true
0
ozoCommented:
al 2 is--->1
0
gudii9Author Commented:
import java.util.HashSet;
import java.util.Set;


public class Test10 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//lastDigit(0,0,9);
		System.out.println("value is---->"+lastDigit(1,11,3));
	}
	public static boolean lastDigit(int a, int b, int c) {
		//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10));
		//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0"));
	//return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");
		
		//public boolean lastDigit(int a, int b, int c) {
			  
			  Set<String> al = new HashSet<String>();
			  System.out.println("al 1 is--->"+Integer.toString(a).substring(Integer.toString(a).length()-1));
			  System.out.println("al 2 is--->"+Integer.toString(b).substring(Integer.toString(a).length()-1));
			  System.out.println("al 3 is--->"+Integer.toString(c).substring(Integer.toString(a).length()-1));
			  
			  al.add(Integer.toString(a).substring(Integer.toString(a).length()-1));
			  al.add(Integer.toString(b).substring(Integer.toString(b).length()-1));
			  al.add(Integer.toString(c).substring(Integer.toString(c).length()-1));
			  System.out.println("al is "+al.toString());
			  return (al.size()<=2);
			  
			}
	}

Open in new window

when i ran above code i got below output

al 1 is--->1
al 2 is--->11//why this 11 is missing
al 3 is--->3
al is [1, 3]//why 11 is missing in this line..i expected this line output as --->al is [1,11,3]
value is---->true
0
ozoCommented:
al.add(Integer.toString(b).substring(Integer.toString(b).length()-1));
System.out.println("al 2 is--->"+Integer.toString(b).substring(Integer.toString(a).length()-1));
0
gudii9Author Commented:
import java.util.HashSet;
import java.util.Set;


public class Test10 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//lastDigit(0,0,9);
		System.out.println("value is---->"+lastDigit(1,11,3));
	}
	public static boolean lastDigit(int a, int b, int c) {
		//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10));
		//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0"));
	//return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");
		
		//public boolean lastDigit(int a, int b, int c) {
			  
			  Set<String> al = new HashSet<String>();
			  System.out.println("al 1 is--->"+Integer.toString(a).substring(Integer.toString(a).length()-1));
			  System.out.println("al 2 is--->"+Integer.toString(b).substring(Integer.toString(b).length()-1));
			  System.out.println("al 3 is--->"+Integer.toString(c).substring(Integer.toString(a).length()-1));
			  
			  al.add(Integer.toString(a).substring(Integer.toString(a).length()-1));
			  al.add(Integer.toString(b).substring(Integer.toString(b).length()-1));
			  al.add(Integer.toString(c).substring(Integer.toString(c).length()-1));
			  System.out.println("al is "+al.toString());
			  return (al.size()<=2);
			  
			}
	}

Open in new window


i corrected as you advised. I am getting below output now.

al 1 is--->1
al 2 is--->1
al 3 is--->3
al is [1, 3]===>i am expecting here output as ===>al is [1,1,3]....not sure why 1 is missing ??
value is---->true
0
ozoCommented:
https://docs.oracle.com/javase/7/docs/api/java/util/Set.html
A collection that contains no duplicate elements.
0
gudii9Author Commented:
right it is HashSet right.

My naming al tricked me thinking that it is arralist
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

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.