Results 1 to 10 of 10

Thread: How can you add only unique items to an array?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269

    How can you add only unique items to an array?

    anyone know?

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    There are a few methods to do this. Do a 'search' using unique array as the keywords.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269
    cant seem to find it anywhere

  4. #4
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918
    Your array would need to be sorted, then before attempting to add a new item, do a binary search of the array. If the item is not found, add it to the array and re-sort the array ready for the next item.

    A bit of mucking round, but it's the only way I know how to do it, and if you pick the right sorting algorithm there's not too much overhead. Of course it depends on the type and volume of data you're storing.
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  5. #5
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Originally posted by VaxoP
    cant seem to find it anywhere
    There were like 140 hits, some of them demonstrated ways to load
    an Array with unique numbers - for example.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269
    but none with strnigs

  7. #7
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Could you not just loop thru the Array to check that the item does/dosn't exist?

    In any event, please be more specific as to what you want/need to acheive

    Does the Array already exist with (string) data, and you want to add if not present?
    Do you want to create a dynamic array of unique (string) data?
    etc....



    Bruce.

  8. #8
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    San Jose, Ca. - USA
    Posts
    302
    the easiest way would be to replace the array with a collection. you can't have duplicate keys in a collection.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269
    how can you set it as a collection, and how can you work with it?

  10. #10
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    San Jose, Ca. - USA
    Posts
    302
    simple
    VB Code:
    1. 'to create the collection
    2. dim col as collection
    3. set col = new collection
    4.  
    5. 'for adding items
    6. col.add item, key
    7.  
    8. 'for retreiving items
    9. col.item(key) or col(key)
    10.  
    11.  
    12. 'for removing items
    13. col.remove key
    14.  
    15. 'to retrieve the count
    16. col.count
    17.  
    18. 'to clear the collection
    19. set col = new collection
    20.  
    21. 'to release the memory held by the collection
    22. set col = nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width