# Determining whether two two-digit numbers share a digit in common

I have solved a CodingBat problem: Given two ints, each in the range 10..99, return true if there is a digit that appears in both numbers, such as the 2 in 12 and 23. (Note: division, e.g. n/10, gives the left digit while the % "mod" n%10 gives the right digit.)

shareDigit(12, 23) → true
shareDigit(12, 43) → false
shareDigit(12, 44) → false


My working code is:

public boolean shareDigit(int a, int b) {
int lefta = a/10;
int righta = a % 10;
int leftb = b/10;
int rightb = b % 10;
if(lefta == leftb || lefta == rightb || righta == leftb || righta == rightb){
}
}


It feels extremely inefficient, and I was wondering if anyone could improve it.

## migrated from stackoverflow.comJan 10 '16 at 17:28

This question came from our site for professional and enthusiast programmers.

You can simplify the boolean parts of your method.

public boolean shareDigit(int a, int b) {
int lefta = a / 10;
int righta = a % 10;
int leftb = b / 10;
int rightb = b % 10;
return lefta == leftb || lefta == rightb || righta == leftb
|| righta == rightb;
}


I think your code is efficient. You just do 4 comparisons. You cannot find answer with less. Maybe instead you can use:

if( (a/10)== (b/10)|| (a/10 )==( b % 10)||  (a % 10)== (b/10) ||  (a % 10) == (b % 10)){