Rajendra Prakash wrote:
The adding will never occur. You are checking for each element inside ret if it's not inside ret. Of course it is, so the guard returns false. This is a good thing, because otherwise the add method would have caused a ConcurrentModificationException. You can't modify a collection while iterating over it (either with an Iterator or with the for-each loop). There may be a few Collection implementations that do allow it but most don't. ArrayList most certainly doesn't.
As for a possible solution:
1) use Collections.sort to sort the list
2) iterate over the loop (using an explicit Iterator!), removing each element (with Iterator.remove(), that's the only way to remove elements while iterating) that equals its predecessor.