Eliminate repeated elements of a vector in C

Asked

Viewed 7,113 times

0

I have to make an algorithm in C that takes user values and armzazena in 2 different vectors (X and Y) and then shows the union (all elements of X and Y without repetitions), the difference (all elements of X that do not exist in Y, without repetitions). But I’m struggling to find the repeated values of the vectors. I found several codes for this purpose but I am not able to understand the logic. Follow what I have 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++;
            }
        }
    }

1 answer

2

Follows an algorithm capable of removing duplicate value elements in a array whole:

#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;
}

Exit:

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 one doesn’t need a array output/auxiliary, look at this:

#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;
}

Exit:

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 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.