find repeated values in the vector in c

Asked

Viewed 61 times

0

I need to create a code that given a number vector it needs to check where it contains repeated values and subtract 1 in the size of the vector whenever it finds, but it is giving time limit error depending on the size of the entries, someone knows a more optimized way to write the code??


int main()
{
   int N, tam;
   scanf("%d", &N);
   int nums[N];
   tam=N;
   
   for(int i=0; i<N; ++i)
       scanf("%d", &nums[i]);
       
   for(int i=0; i<N; ++i){
       for(int j=i+1; j<N; ++j){
           if(nums[i]==nums[j]){
               tam--;
           }
       }
   }
   
   printf("%d", tam);

   return 0;
}
  • If you sort the values you can do in n * log n. And if you take the trouble to build a hashset in C, you can solve in n.

  • Sorting values is more work than finding the number of duplicates. The same goes for creating a hashset. And you still have to count and/or identify duplicates. So it should not be worth it.,,

1 answer

1

About your program

    int N, tam;
    scanf("%d", &N);
    int nums[N];

Do not use this construction. Even if in some case it is possible. In the official state a fixed size or use malloc() and create the size you need, such as

    #define T 20
    int outro[T]; // T é fixo

    // vetor vai de vetor[0] ate vetor[Q-1]...
    int Q = 20;
    int *vetor = (int *)malloc(Q * sizeof(int)); // vetor de 20 int
    free(vetor); // apaga o vetor

Always test the return of scanf(). What’s the point of following if you don’t read anything to N?

I don’t understand the logic of what you’re doing. You need to count the duplicates, but you’re changing tam where you saved the vector size...

Do not write an interactive program. You will only waste time. Use constants or generate the value on the fly. There’s no point in wasting time inventing numbers with each test.

Examples

If you only need the total of duplicates I think you can use

#include <stdio.h>
int main(void)
{
    int teste[] = {1,2,3};
    int dup     = 0;
    int init    = 0; // o primeiro indice
    int N       = sizeof(teste)/sizeof(int);

    for (int L = 0; L < N-1; L += 1)
        for (int R = L + 1; R < N; R += 1)
            if (teste[L] == teste[R])
            {   dup += 1;
                break;
            }
    printf("duplicados = %d\n", dup);
    return 0;
}

And if you need the vector with the single elements you can use something like

#include <stdio.h>
#include <stdlib.h>
void show(int[], int, const char*);

int main(int argc,char** argv)
{
    int teste[20];
    if (argc < 2)
    {
        printf("\
\nentre com os elementos do vetor ao chamar o programa!\n\n");
        return - 1;
    }
    if (argc > 21) argc = 20;
    int N = argc - 1;
    for (int i = 0; i < argc - 1; i += 1) teste[i] = atoi(argv[1+i]);
    int dup     = 0;
    int size    = N;

    show(teste, size, "Vetor original");
    for (int L = 0; L < (N - 1); L += 1)
    {
        for (int R = N - 1; R > L; R -= 1)
        {
            if (teste[L] == teste[R])
            {
                if (R != N - 1) teste[R] = teste[N - 1];
                N -= 1, dup += 1;
            }
        }
    }
    printf("%d elementos duplicados\n", dup);
    show(teste, N, "Elementos unicos");
    return 0;
}

void show(int v[], int n, const char* msg)
{
    if (msg != NULL) printf("%s\n", msg);
    printf("%d elementos: ", n);
    for (int i = 0; i < n; i += 1) printf("%d ", v[i]);
    printf("\n");
};

Running this example

so> p3

entre com os elementos do vetor ao chamar o programa!


so> p3 1 2 3 4
Vetor original
4 elementos: 1 2 3 4
0 elementos duplicados
Elementos unicos
4 elementos: 1 2 3 4

so> p3 1 2 3 4 4
Vetor original
5 elementos: 1 2 3 4 4
1 elementos duplicados
Elementos unicos
4 elementos: 1 2 3 4

so> p3 1 1 2 3 4
Vetor original
5 elementos: 1 1 2 3 4
1 elementos duplicados
Elementos unicos
4 elementos: 1 4 2 3

so> p3 1 2 3 4 3 2 1
Vetor original
7 elementos: 1 2 3 4 3 2 1
3 elementos duplicados
Elementos unicos
4 elementos: 1 2 3 4

so> p3 1
Vetor original
1 elementos: 1
0 elementos duplicados
Elementos unicos
1 elementos: 1

so> 

The notion of duplicate needs a more formal definition

Browser other questions tagged

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