Detect digit repetition in Java [duplicate]

-3

I need to make a program that reads a positive integer N. After reading this number, the program should evaluate if there are repeated numbers in the number.

For example: on 234571 there are no repeated digits. another example: there are 7656 repeated digits.

It has to be done in java and if possible without using vector, my doubt is how to make this check because it can not type a number with equal digits, understood?

package lista01;

import java.util.Scanner;

public class Ex08 {

public static void main(String[] args) {
    Scanner ler = new Scanner(System.in);

    int num = 0, a, b, c, d, contarDigitos = 0;

    System.out.println("Digite um numero de quatro digitos diferentes");
    num = ler.nextInt();

    contarDigitos = (int) (Math.log10(num) + 1);

    if (contarDigitos == 4) {
        d = num / 1000;
        a = (num / 100) % 10;
        b = (num % 100) / 10;
        c = (num % 10) % 10;

        System.out.println("Resultado:" + d + a + b + c);
    } else {
        System.out.println("O numero precisa ter 4 digitos execute novamente!");
    }


   }
}

I do not know how to check if these numbers are different

    
asked by anonymous 28.09.2018 / 17:35

1 answer

0

Well, I thought of a solution that may not be very efficient but at least it does not use vector. Since it has only 10 possible digits, you could have 10 variables (which sounds strange and it would be better to use a 10-position vector, but finally) each one is a flag to know if a given digit has already appeared. For example, int flag1 , int flag2 , and so on. Then in a loop you go through the digits and check if it is the same as the flags. If this happens twice you know the digit has appeared twice.

    
04.10.2018 / 05:40