Nobody
Nobody

Reputation: 159

Check if integer has repeating digits. No string methods or arrays

I'm trying to see if an int has multiples of the same digit. Trying to do it without string methods or arrays. The main method I'm having trouble with is hasDistinctDigits(). It works when the repeating digits are at the end, but not when they come at the beginning or middle.

public static void main(String[] args) {
    System.out.println(hasDistinctDigits(12234));
}

public static boolean hasDistinctDigits(int number) {
    boolean returner = true;
    int count = 1;
    int newNum = number;
    int digit = 0;

    while (count < numDigits(number)) {         
        while (count < numDigits(newNum)) {
            digit = newNum % 10;
            newNum/=10;
            if (digit == getDigit(newNum, count)) {
                returner = false;
            }
            count++;                
        }
        count++;
    }
    return returner;
}

public static int numDigits(int number) {
    int count = 0;
    while (number != 0) {
        number /= 10;
        count++;
    }
    return count;
}

public static int getDigit(int number, int i) {
    int digit = 0;
    int count = 0;
    int originalNum = number;

    while (count <= i) {
        if (count == i) {
            digit = number % 10;
        }
        number /= 10;
        count++;
    }
    if (i > numDigits(originalNum)) {
        return -1;
    } else {
        return digit;
    }
}

}

If this is run, you will see '2' repeats itself, but the method still evaluates to true when it should be false.

Upvotes: 2

Views: 23128

Answers (4)

Densil D&#39;silva
Densil D&#39;silva

Reputation: 1

public static boolean hasDistinctDigits(int number) {
         int numMask = Math.floorMod(number, 10);
         int numDigits = (int) Math.ceil(Math.log10(number+1));
         for (int digitIdx = 0; digitIdx < numDigits; digitIdx++) {
             int curDigit = (int)(number / Math.pow(10,digitIdx)) % 10;
             if(numMask != curDigit)  return false;
             numMask = numMask & curDigit;
         }
         return true;
    }

Upvotes: 0

learner
learner

Reputation: 826

Same logic to verify if a string has unique characters can be used here. (1 << currentChar) , it sets the bit to 1 in currentChar equals to a number(0-9) present at that index and all other bits are set to 0.

(result &(1 << currentChar) : If bit is already set to 1 then return false else

result = result|(1 << currentChar): Set the bit in result integer which is equal to the number at that index.

public class CheckIfDigitsAreRepeated {


        public static void main(String[] args) {
            int input = 1234567897; // false
            // int input = 1234567890;  true

            System.out.println(hasDistinctDigits(input));
        }

        public static boolean hasDistinctDigits(int input){
            int result = 0;

            String inputString = String.valueOf(input);


            for (int i=0; i < inputString.length();i++){

                int currentChar = inputString.charAt(i)- '1';

                if((result &(1 << currentChar)) > 0){
                    return false;
                }

                result = result|(1 << currentChar);

        }

            return true;
        }
    }

Upvotes: 0

xpa1492
xpa1492

Reputation: 1973

Here is a short and sweet version :)

 private static boolean hasDistinctDigits(int number) {
     int numMask = 0;
     int numDigits = (int) Math.ceil(Math.log10(number+1));
     for (int digitIdx = 0; digitIdx < numDigits; digitIdx++) {
         int curDigit = (int)(number / Math.pow(10,digitIdx)) % 10;
         int digitMask = (int)Math.pow(2, curDigit);             
         if ((numMask & digitMask) > 0) return false;
         numMask = numMask | digitMask;
     }
     return true;
 }

It works in a pretty simply way. numMask is an integer used to store what digits have already been encountered (since a decimal system number has only 10 digits and an integer gives use 16-bits, we have enough bits to store each decimal digit as it occurs).

We loop over all digits in the number. For each digit index, we get the actual digit in curDigit. Let's say the current digit is 5. We then check it the 5th bit is raised in out numMask: if it is, then we have already encounter a 5 in the past, so we can immediately tell that the number does not have all distinct digits and return false; otherwise, we modify numMask and raise the 5th bit.

If we make it to the end, then no dupicate digits were encountered.

Upvotes: 8

Tanmay Patil
Tanmay Patil

Reputation: 7057

You need to check each digit with every other digit. This suggests that you should have at least two nested loops. You seem to have mixed them both.

Have one loop for the digit being checked and other for iterating over all other digits.


Also, your getDigit method is not working correctly. Replace it with

public static int getDigit(int number, int i) {
    int digit = 0;
    int count = 0;
    int originalNum = number;

    while (count <= i) {
        if (count == i) {
            digit = number % 10;
        }
        number /= 10;
        count++;
    }
    if (i > numDigits(originalNum)) {
        return -1;
    } else {
        return digit;
    }
}

Hope this helps. Good luck.

Upvotes: 0

Related Questions