# Help in solving algorithm showing repeating figures - Java

4

Dear community, I would like support in solving this algorithm:

Enter any natural number, to show the figures that are repeated and the number of times they are repeated. Example:

``````Ingrese un número natural cualquiera: 471757347
El numero 4: se repite  2 Veces
El numero 7: se repite  4 Veces
``````

I have been able to get the largest or smallest number but only the two, in case of three or four repeated numbers I do not get to solve them. I await your guidance to solve this problem. Thanks.

This is the code I have so far:

``````public static void main(String[] args) {

Scanner linea = new Scanner(System.in);
int n, digito, digito1, repetir;
System.out.print("Introduce un número entero: ");
n = linea.nextInt();
repetir = 0;
digito1 = 0;
do {
digito = n % 10;
if (digito > digito1) {

digito1 = digito;
repetir = 1;
} else {

if (digito == digito1) {
repetir++;
}
}
n = n / 10;

} while (n != 0);

System.out.println("El numero " + digito1 + " se repite " + repetir + " veces");
}
``````

asked by Max 17.06.2016 в 21:15
source

2

If I understand your question well, what you want is to see how many times each digit is repeated in any natural number.

I can think of this solution (not the most efficient, but it works):

``````    public static void main(String[] args) {
Scanner linea = new Scanner(System.in);
long n;
System.out.print("Introduce un número:\t");
n = linea.nextLong();
String num = String.valueOf(n);
for(int d = 0; d < 10; d++) {
int rep = 0;
for(int i = 0; i < num.length(); i++) {
if(Integer.valueOf(String.valueOf(num.charAt(i))) == d) {
rep++;
}
}
if(rep > 0) {
System.out.println(
String.format("El número %d se repite %d %s",
d, rep, (rep == 1 ? "vez" : "veces"))
);
}
}
}
``````

source
2

I do not understand what you want to do with the if:

``````   if (digito > digito1) {
``````

That would serve to find the largest digit, but that has no relevance here. It seems you had copied code from a different problem ...

In any case, you must store somewhere the number of times each digit is repeated. A comfortable way in this case is an array. For example:

`````` public static void imprimirRepetidos(long number) {
int[] cont = new int; // cuenta la cantidad de apariciones de cada digito
int digito;
while(number >0) {
digito = (int) (number % 10);
cont[digito]++;
number /= 10;
}
for(digito=0;digito<10;digito++) {
if(cont[digito]>1)
System.out.println("El digito " + digito + " se repite " + cont[digito]+ " veces");
}
}
``````

Make sure you understand every part of the code.

Another topic, of terminology: do not mix "number", "number" and "digit". Each one of the figures that are used to represent a number in a certain base is called "digit" (it can be decimal, hexadecimal, binary, etc. - in this case, it is assumed decimal).

2

An alternative is to treat the number as a string (obviously validating before it is a number) and with `longitudNumero - longitudPrimerasOcurrencias` you would get the number of times it repeats. I'll give you an example:

``````Map<String, Integer> repeated = new LinkedHashMap<>();
try(Scanner kb = new Scanner(System.in)) {
System.out.print("Ingrese un número: ");
// se asegura que sea un número
Integer number = Integer.parseInt(kb.next());
for(String n : String.valueOf(number).split("")) {
// si el mapa no contiene el número procede
if(!repeated.containsKey(n)) {
int numberLen = number.length();
int replacesLen = number.replace(n, "").length();
int ocurrences = (numberLen - replacesLen);
if(ocurrences > 1) {
repeated.put(n, ocurrences);
}
}
}
} catch(NumberFormatException e) {
System.out.println("No se ha ingresado un número");
System.exit(-1);
}
for(Map.Entry<String, Integer> entry : repeated.entrySet()) {
System.out.printf("\nEl número %s se repite %d veces",
entry.getKey(), entry.getValue());
}
System.out.println();
``````

Exit:

``````Ingrese un número: 471757347

El número 4 se repite 2 veces
El número 7 se repite 4 veces
``````

0

In this case, an analysis of the problem leads to a simple solution:

The digits that can be repeated are 0,1,2,3,4,5,6,7,8,9

A variable that takes the count of its repetitions is needed for each digit. For example:

``````int numeroRepeticionesDigito0 = 0;
//...
int numeroRepeticionesDigito9 = 0;
``````

Now, I see that you already have the way to get each digit separately. It only remains to verify within the cycle that digit is the current one, and increase its variable by one, which can be done with a switch control structure:

``````switch (digito)
{
case 0: numeroRepeticionesDigito0++; break;
case 9: numeroRepeticionesDigito9++; break;
}
``````

And after the cycle finishes, show the value of each one of the variables that carry the total of repetitions. In this case, as I suppose it is a task, I prefer not to show the complete code, but the idea is that, without using advanced instructions, nothing but a structure of repetition (while) and a control (switch). For these kinds of problems, I suggest you analyze the mental process of how you would solve it, and then try to translate that mental process into code.