The exercise tries to compare two arrays with random numbers. the numbers that appear in one array, but in the other, an array with these numbers must not be created, as shown in the example.
Array 1: [5,7,9,2,9,8,3,2,4,8]
Array 2: [6,2,5,8,3,0,5,9,2,0]
Resultat: [7,4,6,0,0]
public class Exercici1 {
public static void main(String[] args) {
Random rand = new Random();
Scanner lector = new Scanner(System.in);
int array1[] = new int[10];
int array2[] = new int[10];
int aux=0;
boolean repetidos[] = new boolean[10];
System.out.println("Array 1:");
for (int i = 0; i < array1.length; i++) {
array1[i] = rand.nextInt(10);
System.out.print(array1[i] + ",");
}
System.out.println();
System.out.println("Array 2:");
for (int i = 0; i < array2.length; i++) {
array2[i] = rand.nextInt(10);
System.out.print(array2[i] + ",");
}
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array2.length; j++) {
if (array1[i] == array2[j]) {
repetidos[i] = true;
break;
}else{
array1[i]=aux;
}
}
}
for (int i = 0; i < array2.length; i++) {
for (int j = 0; j < array1.length; j++) {
if (array2[i] == array1[j]) {
repetidos[j] = true;
break;
}
}
}
System.out.println();
for (int i = 0; i < repetidos.length; i++) {
System.out.print(repetidos[i] + ",");
}
}
}