Win a copy of Spark in Action this week in the Open Source Projects forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

remove duplicates from an array

 
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].
 
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
 
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.
 
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
 
If you were a tree, what sort of tree would you be? This tiny ad is a poop beast.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic