print out non-repeated values

Asked

Viewed 29 times

-1

good evening, I am developing a question, it is almost complete, being able to delete repeated values sequentially, for example: 1 1 2 3 4 5 1, as it excludes 1 and 1 and leaves only 2 3 4 5 1, however, if I type 1 2 3 4 1, it prints this sequence, without removing the repeated ones, what should I change in my code to resolve this? If anyone can help, I’d appreciate it. is annexed hereto:

#include <stdio.h>
#include <string.h>
int main() {
    int vet[10];
    int pos[10];
    int semdup[10];
    int qtd=0; int qtd1=0;
    for (int i = 0; i < 10; ++i) {
        scanf("%d", &vet[i]);
        if (vet[i]>0) {
            pos[qtd++] = vet[i];
        } else {
            printf("Negativo.\n");
        }
    }

    printf("\n");
    for (int i = 0; i < 10; ++i) {
        printf("%d ", vet[i]);
    }

    printf("\n");
    for (int i = 0; i < qtd; ++i) {     
        printf("%d ", pos[i]);
    }

    printf("\n");
    for (int i = 0; i < qtd; ++i) {     
            if (pos[i] == pos[i+1]) {
                pos[i] = pos[i+1];
                        
            } else {
                semdup[qtd1++] = pos[i];
            }
    }
    for (int i = 0; i < qtd1; ++i) {
        printf("%d\n", semdup[i]);
    }
    return 0;
}

1 answer

0


Let’s say you have an array with repeatable and out-of-order elements 1, 1, 2, 3, 4 ,5, 1 and want to print on screen only the values that are not repeated 1, 2, 3, 4, 5.

First, we should organize the array in ascending order, i.e.,1, 1, 1, 2, 3, 4 ,5. For this we go through the array, item by item, and test whether the current item is larger than the next item. If true, we move the highest value item within the array.

Second, we must go through the array to test whether the current item is different from the next item, if it is we must print the value on the screen, resulting in 1, 2, 3, 4, 5.

int main()
{
    int array[] = {1, 1, 2, 3, 4 ,5, 1};
    int size = sizeof array / sizeof *array;
    
    /*ORGANIZA ARRAY */
    for (int i = 0; i < size; ++i) 
    {
        for (int j = i + 1; j < size; ++j)
        {
 
            if (array[i] > array[j]) 
            {
                int a = array[i];
                array[i] = array[j];
                array[j] = a;
            }
 
        }
    }
    
    for(int i = 0; i < size; i++)
    {
        int j = i + 1;
        if(j <= size)
        {
            if(array[i] != array[j])
            {
                printf("%d\n",array[i]);  
            }
        }
    }
    
}
  • I thank you so much for your help, I had not thought of ordering, I’m still a little bad in this area, it worked ordering, thanks.

Browser other questions tagged

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