File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes removing duplicate values from list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "removing duplicate values from list" Watch "removing duplicate values from list" New topic
Author

removing duplicate values from list

abhinas raj
Ranch Hand

Joined: Jun 02, 2012
Posts: 47
how to remove duplicate values from list
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

abhinas raj wrote:how to remove duplicate values from list

abhinas, this site is NotACodeMill (←click).

What have you tried? How do you think you might do it?

ShowSomeEffort (←click) and you're much more likely to get help.

But just as a starter for you, have a look at List.indexOf(Object).

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
abhinas raj
Ranch Hand

Joined: Jun 02, 2012
Posts: 47
i was doing it like first store it in set (since set does not allow duplicate value to be stored), but if i do like that the order of the elements will be changed. so we cant
second method i was trying that match each element with all the previous elements if equal then delete this .. but that also does not look optimized. i want some optimized solution that's why i posted only question
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

abhinas raj wrote:i was doing it like first store it in set (since set does not allow duplicate value to be stored), but if i do like that the order of the elements will be changed. so we cant

That's not necessaily true. A LinkedHashSet does preserve addition order.

second method i was trying that match each element with all the previous elements if equal then delete this .. but that also does not look optimized. i want some optimized solution that's why i posted only question

If this is another interview question, did they ask you to produce an optimized solution? If not, don't worry about it.

Winston
abhinas raj
Ranch Hand

Joined: Jun 02, 2012
Posts: 47
thanks all for your valuable comments
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

abhinas raj wrote:thanks all for your valuable comments

You're welcome.

And BTW, your thinking - ie, to put them in a Set - was spot on; and very "Object-oriented".

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: removing duplicate values from list
 
Similar Threads
Eliminating Duplicate in Arraylist and moving the dups to another arraylist.
How to store unique elements in List?
Help with Data Structures...
Finding "equal" elements in a List.
how to stop the duplicate values.