I have to make an algorithm in C that receives values from the users and the armzazena in 2 different vectors (X and Y) and then shows the union (all elements of X and Y without repetitions), the difference elements of X that do not exist in Y, without repetitions). But I am finding it difficult to find the repeating values of the vectors. I found several codes for this purpose but I can not understand the logic. Here's what I've done:
int main(int argc, char** argv) {
int TAM = 3; //Variavel tamanho para facilitar os testes com valores menores
int x[TAM], y[TAM];
int uniao[TAM * 2], uniao_SR[TAM * 2]; //SR: Sem repetições
int i, j, p = 0;
printf("VETOR X\n");
for (i = 0; i < TAM; i++) {
printf("\tDigite o %dº número: ", i + 1);
scanf("%d", &x[i]);
uniao[i] = x[i]; //União dos dois vetores
}
printf("VETOR Y\n");
for (i = 0; i < TAM; i++) {
printf("\tDigite o %dº número: ", i + 1);
scanf("%d", &y[i]);
uniao[i + TAM] = y[i]; //União dos dois vetores
}
//Tentativa falha de eliminar os valores repetidos
for (i = 0; i < TAM * 2; i++) {
for (j = 1; j < TAM * 2; j++) {
if (uniao[i] != uniao[j]) {
uniao_SR[p] = uniao[j];
p++;
}
}
}