Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Collection of unique ids

Collection of unique ids

Scheduled Pinned Locked Moved C#
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chiman1
    wrote on last edited by
    #1

    I have used

    list<>

    data type to store emp id. As

    List<int> EmpIds = new List<int>() ;

    But it takes duplicate entries. Which data structure I should use to have collection of unique ids.

    L O D 3 Replies Last reply
    0
    • C Chiman1

      I have used

      list<>

      data type to store emp id. As

      List<int> EmpIds = new List<int>() ;

      But it takes duplicate entries. Which data structure I should use to have collection of unique ids.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Have a look at HashSet<T>; it requires .NET 3.5+ :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

      Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

      1 Reply Last reply
      0
      • C Chiman1

        I have used

        list<>

        data type to store emp id. As

        List<int> EmpIds = new List<int>() ;

        But it takes duplicate entries. Which data structure I should use to have collection of unique ids.

        O Offline
        O Offline
        OkkiePepernoot
        wrote on last edited by
        #3

        System.Collections.Generic.HashSet

        1 Reply Last reply
        0
        • C Chiman1

          I have used

          list<>

          data type to store emp id. As

          List<int> EmpIds = new List<int>() ;

          But it takes duplicate entries. Which data structure I should use to have collection of unique ids.

          D Offline
          D Offline
          David Skelly
          wrote on last edited by
          #4

          One slight caveat with HashSet: the data in a HashSet is not sorted. You cannot guarantee what order the entries will be in when you enumerate over them. If that doesn't matter to you, HashSet is great. If the order matters, then you could use SortedSet (which would sort them in numeric sequence). If you need them in some other order (e.g. the order in which they were added) you may be better off building your own UniqueList (which isn't hard to do, just an IList wrapper around List to check if the entry is already there when you add it).

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups