Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

removing duplicate values from list

 
abhinas raj
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to remove duplicate values from list
 
Winston Gutkowski
Bartender
Pie
Posts: 9450
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
abhinas raj
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9450
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all for your valuable comments
 
Winston Gutkowski
Bartender
Pie
Posts: 9450
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic