Deleting duplicates in the array

前端 未结 2 1781
借酒劲吻你
借酒劲吻你 2021-01-25 09:16

I made a program to delete duplicates in an array but the program\'s if condition always remain true. I understood what the problem was,changed arr[i] to arr[count] and allocate

相关标签:
2条回答
  • 2021-01-25 09:26

    You never initialized arr. Currently it's just a pointer, it has no actual bound on it, so you could be overwriting something else.

    Also, you're never incrementing scanf("%d",&arr[i]); I think you want that to read scanf("%d",&arr[counter]);

    0 讨论(0)
  • 2021-01-25 09:49

    To remove duplicates from array create a method, that:

    • sorts the array
    • counts unique values
    • creates a new array, that is of size unique values
    • start coping from 1 array to the other, when their values are different

    To use quicksort in c, you need comparator function like:

    int comp(const void *x, const void *y) {
      return (*(int*)x - *(int*)y);
    }
    

    And then you can call it with:

    qsort(array, 10, sizeof(int), comp);
    

    To count unique items in sorted array, iterate over the array, and do something like:

    if(sortedarray[i]!=sortedarray[i+1]) count++;
    
    0 讨论(0)
提交回复
热议问题