# different numbers java array

3

The exercise consists of the following: given two arrays of 5 positions, we fill them with random numbers from 0 to 10 without repeating (until everything is correct) and then in a third array save the numbers from 1 to 10 have not been saved in any of the 2 arrays. Example: in array1 (1,2,3,4,5) array2 (1,4,2,3,8) in the third array (6,7,9, 10).

this is the code I've tried:

``````    int[] array1 = new int[5];
int[] array2 = new int[array1.length];
int numero = 0;
for (int i = 0; i < array1.length; i++) {
numero = rant.nextInt(10);
if (repetidos(array1, numero) == false) {
array1[i] = numero;
}
}
for (int i = 0; i < array2.length; i++) {
numero = rant.nextInt(10);
if (repetidos(array2, numero) == false) {
array2[i] = numero;
}
}
System.out.println("array2");
for (int i = 0; i < array2.length; i++) {
System.out.print(array2[i]);
}
System.out.println("array1");
for (int i = 0; i < array1.length; i++) {
System.out.print(" " + array1[i]);
}
// guardar en un tercer array los numeros del 1 al 10 que no se hayan
// guardado en ninguno de los 2 arrays
int numero2 = 0;
int numeroc = 10;// numero que se irá comparando a medida que vaya
// avanzando
int posiciones = 0;
for (int i = 0; i < array2.length; i++) {
for (int j = 0; j < array1.length; j++) {
if (array1[j] == numeroc || array2[i] == numeroc) {
numeroc--;
} else if (array1[j] != numeroc || array2[i] != numeroc) {
posiciones++;
}
}
}
int[] array3 = new int[posiciones];// medida de el array definitivo

for (int i = 0; i < array2.length; i++) {
for (int j = 0; j < array1.length; j++) {
for (int b = 0; b < array3.length; b++) {

if (array1[j] == numeroc || array2[i] == numeroc) {
numeroc--;
} else if (array1[j] != numeroc || array2[i] != numeroc) {
array3[b] = numeroc;
}
}
}
}
System.out.println("array3");
for (int i = 0; i < array3.length; i++) {
System.out.print(array3[i]);
}

}

public static boolean repetidos(int[] array, int numero) {

boolean repetido = false;
for (int i = 0; i < array.length; i++) {
if (array[i] == numero) {
repetido = true;

}
}
return repetido;
``````

and the output is as follows: array2 46810array1  9 3 1 0 2array3 10101010101010101010101010101010101010101010101010

What is the failure? What exactly have I failed? Is there any other way to do it?

asked by david sierra fernandez 17.05.2018 в 22:18
source

5

You can do it easier. You already have a function that tells you if an item is in an array, you just have to use it like this:

``````int b = 0;
for (int i = 0; i < 10; i++) {
if(!repetidos(array1, i) && !repetidos(array2, i) {
array3[b] = i;
b++;
}
}
``````

answered by 17.05.2018 / 22:35
source
2

Your code has a problem.

The loop you use to fill array1 and array2 with random numbers has a failure because it leaves positions unfilled (to zero) when it is the case that a number is repeated. One solution is the following:

``````for (int i = 0; i < array1.length;) {
numero = rant.nextInt(10);
if (arrayContieneNumero(array1, numero) == false) {
array1[i] = numero;
i++;
}
}
``````

You only have to increase the control variable when a number is not repeated . That's why I took it out.

On the other hand, the solution proposed by alanfcm seems right .

Greetings, David.

answered by 17.05.2018 в 23:13