Is there any method to remove the duplicate elements in an array in place in C/C++ in O(n)?
Suppose elements are a[5]={1,2,2,3,4}
then resulting array should contain {1,2,3,4}
The solution can be achieved using two for loops but that would be O(n^2) I believe.
Is there any method to remove the duplicate elements in an array in place
Share
If, and only if, the source array is sorted, this can be done in linear time:
Otherwise you’ll have to sort first, which is (99.999% of the time)
n lg n.