Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

remove duplicates from an array

 
Pradeep Chauhan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I have an array, String []
I have to remove duplicates from this array. I can use a temp array.
Could anybody guide me as how will I do this?
_pradeep
 
Pradeep Chauhan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my array is 2d, let's say String [i][2] and I have to remove all the duplicates for
String[i][0] and String[i][1].
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this homework? Have you tried anything yet? If you've tried something and you're stuck, let us see what you've done and we'll help.
In addition, since this doesn't really pertain to the SCJP certification, I'm moving this to Java in General (Beginner).
Corey
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have to remove duplicates from this array.

Are the duplicates allowed in the first place? If not, a HashSet may be a more appropriate data structure. If you insist on using an array, consider an ArrayList collection.
Eugene.
[ March 31, 2003: Message edited by: Eugene Kononov ]
 
Pradeep Chauhan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No Duplicates are not allowed in first place. Actually first place is a key.
Had it been one-dimesional array, I would have used HashSet. I don't know for 2d array.
Could anybody help me as I'm not an experienced guy in java.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Had it been one-dimesional array, I would have used HashSet.

What Eugene is getting at is you can put the Strings in a HashSet (or any Set for that matter) and attempting to add a duplicate will have no effect since Sets do not allow duplication.
Michael Morris
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic