To delete duplicate elements in an array, we first need to find duplicate elements in the array. For this, we check each element with all other elements present after its index value. If we find that there is a duplicate then we simply swap that duplicate element with the array[size-1] (last) element of the array and reduce the value of size by 1.

We reduce the value of size by 1 because we no more require to traverse that element since it is already a duplicate of another element which we already considered or included in the array.

By doing this for every item in the array, we are shifting all duplicates to the right of the array. And printing only the left part of the array which doesn’t contain any duplicate element.

Output

Delete Duplicate Elements in an Array in C

That’s all we need to write to delete duplicate numbers in the array in C. If you didn’t understand code then comment below your doubts.

Leave a Reply

Close Menu