Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse Integer number without duplicate digit in java

My objective is to reverse integer number without duplicate digit in Java How do I improve the complexity of code or is there a good/standard algorithm present?

Incase of duplicate digit, it should preserve last digit

public static void main(String[] args) {
    int n = -19890;
    System.out.println(reverseNumberWithoutDuplicate(n));
}

public static int reverseNumberWithoutDuplicate(int number) {
    boolean isNegative = (number < 0);
    number = isNegative ? number * -1 : number;

    Set<Character> lookup = new HashSet<>();

    StringBuffer sb = new StringBuffer();
    char[] digits = String.valueOf(number).toCharArray();
    for (int i = digits.length - 1; i >= 0; --i) {
        if (lookup.contains(digits[i])) {
            continue;
        }
        sb.append(digits[i]);
        lookup.add(digits[i]);
    }
    return isNegative ? Integer.parseInt(sb.toString()) * -1 : Integer.parseInt(sb.toString());
}

Expected output: -981

like image 720
Saurabh Khare Avatar asked Nov 07 '16 19:11

Saurabh Khare


People also ask

How do you reverse a 4 digit number in Java?

if (num < 0 || num > 9999) throw new IllegalArgumentException("Not a 4-digit number: " + num); int reverse = 0; for (int i = 0; i < 4; i++, num /= 10) reverse = reverse * 10 + num % 10; System. out. printf("%04d%n", reverse);

How do you reverse an integer without strings?

You can use recursion to solve this. first get the length of an integer number by using following recursive function. and then you can simply multiply remainder of a number by 10^(Length of integer - 1).


2 Answers

Let's build a solution step-by step. The following function reverses digits of the positive number.

int reverseNumber(int number) {
    int answer = 0;
    for (int n = number; n != 0; n /= 10) {
        // Digits are taken from least significant to most significant
        int digit = n % 10; 
        // And added the other way round
        answer = answer * 10 + digit;
    }
    return answer;
}

This code could be easily adapted to work with negative numbers to:

int reverseNumber(int number) {
    if (number < 0) {
        return -reverseNumber(-number);
    }
    // The rest is the same

Our next target -- skip duplicate digits. We will track a list of the already seen digits in the boolean[] seen.

private static int reverseNumberWithoutDuplicate(int number) {
    if (number < 0) {
        return -reverseNumberWithoutDuplicate(-number);
    }

    boolean[] seen = new boolean[10];
    int answer = 0;
    for (int n = number; n != 0; n /= 10) {
        int digit = n % 10;            
        if (!seen[digit]) {
            seen[digit] = true;
            answer = answer * 10 + digit;
        }
    }
    return answer;
}
like image 94
kgeorgiy Avatar answered Sep 28 '22 16:09

kgeorgiy


The complexity is fine. Though it can be optimized.

Using StringBuilder is better than the older StringBuffer, which has unneeded overhead (for thread-safeness).

Then the data can remain numerical, and for the ten possible digits a BitSet is just fine.

public static int reverseNumberWithoutDuplicate(int number) {
    if (number == Integer.MIN_VALUE) {
        // -2147483648 is a special case, not negatable.
        return -8463712;
    }
    boolean isNegative = number < 0;
    number = isNegative ? -number : number;

    BitSet usedDigits = new BitSet(10);
    int reversedNumber = 0;
    while (number != 0) {
        int digit = number % 10;
        number /= 10;
        if (!usedDigits.get(digit)) {
            usedDigits.set(digit);
            reversedNumber = 10 * reversedNumber + digit;
        }
    }
    return isNegative ? -reversedNumber : reversedNumber;
}
like image 36
Joop Eggen Avatar answered Sep 28 '22 16:09

Joop Eggen