0

I have to do a function which, taken as parameters a pointer (int *vect) and the dimension (dim) of the array allocated, returns a new array which contains all the elements of the old array not repeated and all the first occurrence of repeated elements, this is the code where i put the elements not repeated of the first array, but i don't know how to proceed to put the first occurrence of repeated elements (e.g. INPUT : vect={1;2;3;3;4;5;5} OUTPUT : {1;2;3;4;5})

int* deleteDup(int *vect,int dim){
    int* vect2,int dim2;
    vect2=malloc(sizeof(int)*dim2);
    int i,j,temp;
    int count=0;
    for(i=0;i<dim-1;i++){
        temp=vect[i];
        for(j=i+1;j<dim;j++){
            if(temp==vect[j]){
            count++;
        }
    }
    if(count==0){
        dim2++;
        vect2=realloc(vect2,dim2*sizeof(int));
        vect2[dim2]=temp;
    }
    *dim_nodup=dim2;
    return vect2;
}
4
  • Must the original order be maintained? Commented Aug 29, 2017 at 11:06
  • Yes and the first array must don't be modified Commented Aug 29, 2017 at 11:09
  • Are the entries sorted? Your sample input looks like that. Commented Aug 29, 2017 at 11:13
  • nono sorry, the entries are not sorted, they are randomic Commented Aug 29, 2017 at 11:15

1 Answer 1

1

This looks like homework to me.

You need to first count the number of unique elements, then allocate an array of that size, and then move one instance of each unique element to the new array.

This can be optimised various ways.

I prefer not to give you code or even pseudo code, as you should figure this out yourself when solving the problem.

i don't know how to proceed to put the first occurrence of repeated elements

To do this you must move elements one by one into the array you are returning, and for each item check if it is already in the array.

Good luck! :)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, i understood the problem i had, now i think i will solve the problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.