Deleting duplicates in the array

前端 未结 2 1831
借酒劲吻你
借酒劲吻你 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: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++;
    

提交回复
热议问题