From two arrays make a new one but with the numbers that are not repeated between the two arrays [duplicated]

2

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] + ",");

        }
    }

}
    
asked by GotenkSSS7 27.02.2018 в 19:48
source

2 answers

0

It is necessary to go through the array a asking if the value exists in the array b , in the true case the value is added to the array resultado .

var a = [5,7,9,2,9,8,3,2,4,8];
var b = [6,2,5,8,3,0,5,9,2,0];
var resultado = [];

a.forEach(x =>{
    if(!b.includes(x)){
        resultado.push(x)
    }
});

b.forEach(x =>{
    if(!a.includes(x)){
        resultado.push(x)
    }
});

In the case only arrays should be used:

public void extract() {
    int[] a = randomIntArray(10);
    int[] b = randomIntArray(10);

    int[] resultado = compare(a, b, new int[] {});
    resultado = compare(b, a, resultado);

    for (int i = 0; i < resultado.length; i++) {
        System.out.println(resultado[i]);
    }
}

public int[] compare(int[] a, int[] b, int[] resultado) {
    for (int i = 0; i < a.length; i++) {
        boolean match = false;
        for (int e = 0; e < b.length; e++) {
            if (a[i] == b[e]) {
                match = true;
            }
        }
        if (!match) {
            int[] temp = resultado;
            resultado = new int[temp.length + 1];
            for (int j = 0; j < temp.length; j++) {
                resultado[j] = temp[j];
            }
            resultado[temp.length] = a[i];
        }
    }
    return resultado;
}

public int[] randomIntArray(int lenght) {
    int[] array = new int[lenght];
    for (int i = 0; i < array.length; i++) {
        array[i] = random(0, 9);
    }
    return array;
}

public int random(int min, int max) {
    return (int) (Math.random() * ((max - min) + 1)) + min;
}
    
answered by 27.02.2018 в 20:11
0

Try this: Find in an array those that are not repeated in the other and vice versa, and add them in a third array.

public static void main(String[] args) {
Random rand = new Random();
int array1[] = new int[10];
int array2[] = new int[10];
int repetidos[] = new int[20];
int repetido=0;
boolean rep=true;

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] + ",");

}
System.out.println();

for (int i = 0; i < array1.length; i++) {
    rep = false;
    for (int j = 0; j < array2.length; j++) {
        if (array1[i] == array2[j]) {
            rep = true;
            break;
        }

    }
    if(rep==false) {
        repetidos[repetido] = array1[i];
        repetido++;
    }
}

for (int i = 0; i < array2.length; i++) {
    rep=true;
    for (int j = 0; j < array1.length; j++) {
        if (array2[i] == array1[j]) {
            rep = false;
            break;
        }
    }
    if(rep==false) {
        repetidos[repetido] = array2[i];
        repetido++;
    }
}

System.out.println();
for (int i = 0; i < repetidos.length; i++) {
    System.out.print(repetidos[i] + ",");

}
}
    
answered by 27.02.2018 в 20:21