Java > Logic-1 > lastDigit (CodingBat Solution)

Problem:

Given three ints, a b c, return true if two or more of them have the same rightmost digit. The ints are non-negative. Note: the % "mod" operator computes the remainder, e.g. 17 % 10 is 7.

lastDigit(23, 19, 13) → true
lastDigit(23, 19, 12) → false
lastDigit(23, 19, 3) → true


Solution:

public boolean lastDigit(int a, int b, int c) {
  int modA = a % 10;
  int modB = b % 10;
  int modC = c % 10;
  
  if (modA == modB || modA == modC || modB == modC)
    return true;
  else
    return false;
}

7 comments:

  1. public boolean lastDigit(int a, int b, int c) {
    int modA = a % 10;
    int modB = b % 10;
    int modC = c % 10;

    return (modA == modB || modA == modC || modB == modC);
    }

    Or you could do an one linear

    ReplyDelete
  2. return a % 10 == b % 10 || a % 10 == c % 10 || c % 10 == b % 10;

    ReplyDelete
  3. int aR = a % 10;
    int bR = b % 10;
    int cR = c % 10;

    return !(aR != bR && bR != cR && aR != cR);

    ReplyDelete
  4. public boolean lastDigit(int a, int b, int c) {
    int aRem = a%10;
    int bRem = b%10;
    int cRem = c%10;
    if(aRem != bRem && bRem != cRem && aRem != cRem){
    return false;
    }
    return true;
    }

    ReplyDelete
  5. public boolean lastDigit(int a, int b, int c) {
    return (a % 10 == b % 10) != (b % 10 == c % 10) != (c % 10 == a % 10);
    }

    ReplyDelete
  6. if((a % 10 == b % 10)||(a % 10 == c % 10) || (b % 10 == c % 10)) return true;
    else return false;

    ReplyDelete
  7. return((a%10==b%10||a%10==c%10||b%10==c%10));

    ReplyDelete