Delete repeated elements of a vector in C

0

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++;
            }
        }
    }
    
asked by anonymous 28.05.2018 / 19:41

1 answer

2

Following is an algorithm that can remove duplicate value elements in an array of integers:

#include <stdlib.h>
#include <stdio.h>


int main( void )
{
    /* Vetor de entrada com elementos de valor duplicado */
    int vetor[14] = { 1, 2, 3, 4, 4, 5, 5, 6, 7, 8, 9, 9, 10, 10 };

    /* Vetor de saida */
    int saida[14];

    int i, j, n = 0;
    int tam = sizeof(vetor) / sizeof(int);

    /* Exibe Vetor de Entrada */
    printf("Antes: ");
    for( i = 0; i < tam; i++ )
        printf("%d ", vetor[i] );
    printf("\n");

    /* Remove elementos com valor duplicado */
    for( i = 0; i < tam; i++)
    {
        for( j = 0; j < n; j++ )
        {
            if( vetor[i] == saida[j] )
                break;
        }

        if( j == n )
        {
            saida[n] = vetor[i];
            n++;
        }
    }

    /* Exibe Vetor de Saida */
    printf("Depois: ");
    for( i = 0; i < n; i++ )
        printf("%d ", saida[i] );
    printf("\n");

    return 0;
}

Output:

Antes: 1 2 3 4 4 5 5 6 7 8 9 9 10 10 
Depois: 1 2 3 4 5 6 7 8 9 10 

You can use a similar algorithm, but this does not need an output / auxiliary array, see:

#include <stdlib.h>
#include <stdio.h>


int main( void )
{
    /* Vetor com elementos de valor duplicado */
    int vetor[] = { 1, 2, 3, 4, 4, 5, 5, 6, 7, 8, 9, 9, 10, 10 };

    int i, j, k;
    int tam = sizeof(vetor) / sizeof(int);

    /* Exibe Vetor Antes */
    printf("Antes: ");
    for( i = 0; i < tam; i++ )
        printf("%d ", vetor[i] );
    printf("\n");

    /* Remove elementos com valor duplicado */
    for( i = 0; i < tam; i++ )
    {
        for( j = i + 1; j < tam; )
        {
            if( vetor[j] == vetor[i] )
            {
                for( k = j; k < tam; k++ )
                    vetor[k] = vetor[k + 1];

                tam--;
            }
            else
            {
                j++;
            }
        }
    }

    /* Exibe Vetor Depois */
    printf("Depois: ");
    for( i = 0; i < tam; i++ )
        printf("%d ", vetor[i] );
    printf("\n");

    return 0;
}

Output:

Antes: 1 2 3 4 4 5 5 6 7 8 9 9 10 10 
Depois: 1 2 3 4 5 6 7 8 9 10 
    
28.05.2018 / 19:58