1

Possible Duplicate:
Array remove duplicate elements

This is a homework question. I think the best way is a modified merge sort, where the modified merge just does not copy duplicate elements from the two input arrays to the result array. The complexity is O(N*logN) (exactly as the regular merge sort). Does it make sense ? Are there any better solutions ?

asked Jan 2, 2011 at 6:06
1

3 Answers 3

4

If you do not need to sort, only remove the duplicates it can be done in O(N) time by using a HashSet (or your language equivalent).

answered Jan 2, 2011 at 6:12
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, I do not need them sorted. I can just copy the array elements into a set and copy the set into a array. BTW, one can use TreeSet to have the array sorted.
Be careful. If you use TreeSet, you will revert back to O(n*log(N)).
However, sorting allows n-way sorting or similar to be used, which is very handy if the data-set does not fit (well) into memory. But that's already into the realm of "other problems" :p
@treenplusone Thanks, you are right about TreeSet.
HashSet does a lookup in constant time for most datasets, but the bound is actually much worse.
1

Sounds good. Or you can do any kind of sort and then remove duplicates with complexity O(N)...

answered Jan 2, 2011 at 6:09

Comments

0

First you must Sorted the array Since it is a sorted array any duplicates would be next to each other. So start at the beginning and compare each element to it neighbor on its right. If there are no duplicates then you have made n-2 comparisons and you are done.

If you find a duplicate you are going to have a hole. Since it is an array an not a link list you will need to move every element down. (You could create a new array if that was allowed.) However you do not just want to start moving everything down, you can simply mark with a pointer or counter where the hole is and move on to the next comparison. When you get the next non-duplicate you move(copy) that element into the hole and increment your hole counter by 1.

answered Jan 2, 2011 at 14:57

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.