0

I'm having a difficult time with my program! For this method I have to check to see if all the numbers are distinct and I can't figure out for the life of me what I am doing wrong. I don't know if using an array is the best way to go. I must call the getDigit method.

for (int i = 0; i <= numDigits(number); i++) {
    int digit = getDigit(number,i);
    if (digit == getDigit(number,i)) {
        return false;
    }
}
return true;
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • 4
    Your method always returns `false`, because `getDigit(number,i) == getDigit(number,i)` is always `true`. – Konstantin Yovkov Nov 07 '14 at 12:52
  • What is `numDigits()`? and `getDigit()`? – Aniket Kulkarni Nov 07 '14 at 12:52
  • int digit = getDigit(number,i); -> digit == getDigit(number,i) will always be true – Mirco Nov 07 '14 at 12:52
  • And that's only one of the problems. – biziclop Nov 07 '14 at 12:53
  • use [this](http://stackoverflow.com/questions/5196186/split-int-value-into-separate-digits) to split the number into digits, then use [this](https://social.msdn.microsoft.com/Forums/vstudio/en-US/d0a8f9c6-2e14-4831-afb6-5e30b00bd69c/how-to-find-if-integer-array-include-duplicate-values) to see if there are any duplicates – jbutler483 Nov 07 '14 at 12:54
  • @verbose-mode you assume too much, `getDigit` is black box, how you can be sure is not returning random integer – user902383 Nov 07 '14 at 12:54
  • `number % 10` returns the most right digit of the number. Increase `10` to `100`, `1000` and so on to get all the digits and compare them – mastaH Nov 07 '14 at 12:54
  • public static int getDigit(int number, int i) { int negative =-1; int counter = 0; int digit = 0; if(i>numDigits(number)|| i == 0) { return negative; } while(counter < i) { digit = number % 10; number = number / 10; counter++; } – Amanda Merical Nov 07 '14 at 12:58
  • numDigits just counts the length of the number, getDigits gets the ith digit of the number. – Amanda Merical Nov 07 '14 at 12:59

4 Answers4

1

You can first get each digit from the number and add them to a HashSet, then compare the size of HashSet with the number of digits present in the number

You can try this code:

public static void main(String[] args) {
    int val = 123554;
    Set<Integer> set = new HashSet<Integer>(); // HashSet contains only unique elements
    int count = 0;       // keeps track of number of digits encountered in the number
  // code to get each digit from the number
    while (val > 0) {                           
        int tempVal = val % 10;
        set.add(tempVal);         // add each digit to the hash set
// you can have a boolean check like if(!set.add(tempVal)) return false; because add() returns false if the element is already present in the set.
        val = val / 10;
        count++;
    }

    if (count == set.size()) {
        System.out.println("duplicate digit not present");
    } else {
        System.out.println("duplicate digit present");
    }
}
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

Splitting Int into single digits:

Use something similar to this:

Code to print the numbers in the correct order:

int number; // = and int
LinkedList<Integer> stack = new LinkedList<Integer>();
while (number > 0) {
    stack.push( number % 10 );
    number = number / 10;
}

while (!stack.isEmpty()) {
    print(stack.pop());
}

Source

Checking for Duplicates:

Again, something similar to this:

public static boolean duplicates (int [] x, int numElementsInX ) {
    Set<Integer> set = new HashSet<Integer>();
    for ( int i = 0; i < numElementsInX; ++i ) {
        if ( set.contains( x[i])) {
            return true;
        }
        else {
            set.add(x[i]);
        }
    }
    return false;
}

Source


Alternative

If you can split the array, an alternative could be to use:

int[] numbers = { 1, 5, 23, 2, 1, 6, 3, 1, 8, 12, 3 };
Arrays.sort(numbers);

for(int i = 1; i < numbers.length; i++) {
    if(numbers[i] == numbers[i - 1]) {
        System.out.println("Duplicate: " + numbers[i]);
    }
}
Community
  • 1
  • 1
jbutler483
  • 24,074
  • 9
  • 92
  • 145
0

i suppose that you want to compare for example the number 12345 with 23145, and prompt out a false, and if they are the same (digit by digit, prompt a true) , am i right?. If you want to do this, you should make 2 arrays and you have to make sure to compare each position of both so you can compare digit by digit.

Hope it helps you

  • I want to get a number ex 1123 and return true because 1 appears twice – Amanda Merical Nov 07 '14 at 13:07
  • Ok, So you are looking for in a determined number, get X times of mach and then print true, So you better do : `int[] numbers; //you can initialize the array as you want like @jbutler483 //said,or read from keyboard with Scanner keyboard = new Scanner(System.in); System.out.println("enter an integer:"); int numbers = keyboard.nextInt(); for(int i = 0; i <= numbers.length; i++) { if(numbers[i] == numbers[i - 1]) { System.out.println("Duplicate: " + numbers[i]); }` its better for you to use linked lists so you can compare the 1º with the other numbers of a number – G0rd0l0D3v Nov 07 '14 at 13:30
0
public boolean unique(int theNumber) {
    String number = new Integer(theNumber).toString();
    Set<Character> set = new LinkedHashSet<Character>();
    for(char c:number.toCharArray()) {
        set.add(Character.valueOf(c));
    }
    return number.length() == set.size();

}
Ivan T
  • 71
  • 2