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. Combine duplicates from an ArrayList

Combine duplicates from an ArrayList

Scheduled Pinned Locked Moved C#
question
2 Posts 2 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.
  • I Offline
    I Offline
    InvalidTypecast
    wrote on last edited by
    #1

    For some reason I can't figure this out Let's say I have an ArrayList of a class that has a string productName and an int called count. I want to basically combine the duplicates in the following way. productName | count ------------------- Product1 | 1 Product2 | 1 Product1 | 1 After combining I'd want it to look like productName | count ------------------- Product1 | 2 Product2 | 1 What is my best bet?

    M 1 Reply Last reply
    0
    • I InvalidTypecast

      For some reason I can't figure this out Let's say I have an ArrayList of a class that has a string productName and an int called count. I want to basically combine the duplicates in the following way. productName | count ------------------- Product1 | 1 Product2 | 1 Product1 | 1 After combining I'd want it to look like productName | count ------------------- Product1 | 2 Product2 | 1 What is my best bet?

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      Hello, Let me suggest a redesign. Instead of ArrayList you could use a Hashtable (or Dictonary .Net>1.1). Your "Value" would be the full instance of the class, and the "Key" would be the productName. You could than make an "Add" Method which controls if the Key already exist. If no -> add the instance to the Collection. If yes -> increase the count of the existing instance. Hope it helps! All the best, Martin

      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