aspose 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


Win a copy of Spring in Action this week in the Spring forum!
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: 8008
    
  22

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: 8008
    
  22

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: 8008
    
  22

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
 
wood burning stoves
 
subject: removing duplicate values from list