Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Array problem

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone tell me how i get rid of all duplicated values in an array?
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It ain't pretty...

...and it ain't all that ugly.
Good Luck.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Classes that implement the Set interface do just what you want to do. I used TreeSet here that also sorts the elements of the set. HashSet implements Set and doesn't sort its elements.
 
Peter Phung
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the code work with strings too?
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An array of what exactly, what does "duplicate" mean (equals(), ==), how large do you expect the array to be, can you invalidate individual elements to leave "holes", and how important is performance?
Knee-jerk reaction would be to suggest that an array might possibly be an inappropriate data structure for your problem and that you consider replacing it by a Set, probably a HashSet.
If you can't replace (or convert) the array with (to) something else, for instance because it's an array of primitives and you can't afford to wrap each element inside an object, there are basically three ways to go about junking duplicates.
  • Sort it (eg Arrays.sort()) so that duplicates occupy consecutive slots, you can then easily invalidate them in a linear scan. Performance is O(n log n).
  • Scan through the array (1..n), comparing each element i to elements i+1 .. n and zapping any duplicates. Simple, but not efficient (O(n^2)).
  • Scan through the array (1..n), putting each element in a hash table as you go along (either a HashSet or a hash structure you write yourself), flagging up any duplicates. Very efficient (O(n)) but eats more memory.
  • If you can't invalidate elements (no holes), you will have to compact your array as you go through it.
    - Peter
     
    Dirk Schreckmann
    Sheriff
    Posts: 7023
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Does the code work with strings too?


    You're kidding, right?
    If - you - were - to - try - it - all - by - yourself, you'd find that, yes, it works for all objects that implement Comparable (at least I think that's the stipulation). But, then why would you do it yourself when you have an idiot to do it for you?

    [ March 13, 2002: Message edited by: Dirk Schreckmann ]
     
    Peter den Haan
    author
    Posts: 3252
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hey, those answers weren't there when I started writing
    A Collection such as a HashSet or a TreeSet will work with any class that fulfills the java.lang.Object contract for equals() and hashCode(). In the case of TreeSet, either the class should implements Comparable or you must supply a suitable Comparator.
    The java.lang.String class satisfies all requirements. So yes, it will work.
    - Peter
     
    Dirk Schreckmann
    Sheriff
    Posts: 7023
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    And I was just about to give some pseudo-code similar to what you gave, but far less intelligent sounding and without the efficiency analysis.
    Let's see, how many times can we trip over each other's threads...
     
    Peter Phung
    Ranch Hand
    Posts: 138
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    can anyone tell me how to output one string at a time if the strings are all being held in a treeset? cos i need to print some values between each of the strings in the treeset.
     
    Dirk Schreckmann
    Sheriff
    Posts: 7023
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Looking at the documentation for TreeSet in The Java API Specification we see what methods we have available for operating on an instance of the class TreeSet.
    If you'd rather work with a normal array, you might be interested in the toArray() method, which does just what it sounds like it might do.
    first() returns the first (lowest) element currently in this sorted set.
    last() returns the last (highest) element currently in this sorted set.
    remove(Object o) removes the specified element from this set if it is present.
    Good Luck.
    [ March 14, 2002: Message edited by: Dirk Schreckmann ]
     
    Peter Phung
    Ranch Hand
    Posts: 138
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    if i use the first() method does the first value in the set get erased? i.e. is calling the first() method like placing a pointer at the start of the set ,getting that value, and then moving the pointer onto the next value in the set.
    the API specification isn't very clear on this.
     
    Dirk Schreckmann
    Sheriff
    Posts: 7023
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Dirk Schreckmann:
    If - you - were - to - try - it - all - by - yourself ... But, then why would you do it yourself when you have an idiot to do it for you?



    [ March 14, 2002: Message edited by: Dirk Schreckmann ]
     
    Dirk Schreckmann
    Sheriff
    Posts: 7023
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Perhaps you want to learn about the Stack class.
    [ March 14, 2002: Message edited by: Dirk Schreckmann ]
     
    Peter Phung
    Ranch Hand
    Posts: 138
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sorry about all the stupid questions, but i don't understand whats going wrong with my code
    here it is:

    the problem is when i try to count the no of tokens, the right value is returned but the no of values assigned into the array is 1.
    Please can u tell me what is going wrong
     
    Wanderer
    Posts: 18671
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Each time through the loop, you're calling nextToken() twice. This cases you to advance through the list of tokens twice as fast as you want. Call it once, and save the value returned in a varaible so you can re-use it.
     
    Peter Phung
    Ranch Hand
    Posts: 138
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for the help so far guys. I appreciate it, but i'm having a bit of a problem with StringTokenizer. here's my code :

    it is my understanding that the code should work but the variable countParam is 1 when it should be 6. can anyone tell me why this is?
     
    Peter Phung
    Ranch Hand
    Posts: 138
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ignore my last post, the problem was me not using my eyes.
    Thanks for all the help.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic