I would suggest a combination of those two methods. Use a second array, and add each element if it has not already been added. Use the set to maintain those already added. That will run in linear time, whilst using an array on its own runs in quadratic time. Using a Set in tandem with a List (maybe
this) will probably allow you to do the whole thing in linear time, and maintain ordering, too.