• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1975
  • Last Modified:

Merge Hashtables but remove duplicates?

I have two hashtables:

Hashtable ht1 = new Hashtable();

ht1.Add("1","one");
ht1.Add("2","two");
ht1.Add("3","three");
ht1.Add("4","four");

Hashtable ht2 = new Hashtable();

ht2.Add("1","one");
ht2.Add("3","three");
ht2.Add("5","five");
ht2.Add("7","seven");

I need to compare the two hashtables and create a THIRD (new) hashtable that contains a merged set of keys/values from the two hashtables but removes any duplicates from the merged hashtable (unique keys only).  So after merging the resulting hash table would contain:

"1","one"
"2","two"
"3","three"
"4","four"
"5","five"
"7","seven"

Is there a merge method for the hashtable?  If not, any suggestions on how to go about this?

Thanks!
0
djs120
Asked:
djs120
  • 4
  • 2
1 Solution
 
b1xml2Commented:
     public sealed class HashtableProcessor
      {
            private HashtableProcessor() {}
            
            public static Hashtable Add(Hashtable first,Hashtable second)
            {
                  Hashtable table = new Hashtable();
                  foreach(DictionaryEntry e in first)
                  {
                        table.Add(e.Key,e.Value);
                  }

                  foreach(DictionaryEntry e in second)
                  {
                        if (! table.ContainsKey(e))
                              table.Add(e.Key,e.Value);
                  }

                  return table;
            }
      }
}
0
 
b1xml2Commented:
using System;
using System.Collections;

Hashtable merged = HashtableProcessor.Add(h1,h2);
0
 
djs120Author Commented:
While waiting for a response I wrote the following and it seems to work:

                        Hashtable biMerged = new Hashtable();
                        Hashtable biHash1 = /* FILLS HASHTABLE1 */
                        Hashtable biHash2 = /* FILLS HASHTABLE2 */

                        biMerged = biHash1; // copy over hash1 to merged, then roll through bash2

                        IDictionaryEnumerator myEnum = biHash2.GetEnumerator();

                        while (myEnum.MoveNext())
                        {
                              if (!biMerged.ContainsKey(myEnum.Key))
                              {
                                    biMerged.Add(myEnum.Key,biHash2[myEnum.Key]);
                              }
                        }

Let me know if you see problems with this code.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
b1xml2Commented:
    biMerged = biHash1; <-- is not a copy but a reference blMerged is still blHash1, so if you add anything to blHash1, it will appear in merged.
0
 
gseidmanCommented:
If you don't care about values, only keys, you can just add them sequentially without worrying about it:

HashTable mergeHashTables(HashTable a, HashTable b) {
  HashTable result = new HashTable(a.Count+b.Count);
  foreach (DictionaryEntry entryA in a) {
    result[entryA.Key] = entryA.Value;
  }
  foreach (DictionaryEntry entryB in b) {
    result[entryB.Key] = entryB.Value;
  }
  return result;
}
0
 
djs120Author Commented:
What is the best way to copy one hashtable to another and maintain both keys and values?  I just wrote this:

foreach(DictionaryEntry de in originalHash)
{
  newHash.Add(de.Key, de.Value);
}
0
 
b1xml2Commented:
hmm you can copy accordingly

look at the constructor:

                  Hashtable first = new Hashtable();
                  first.Add("12","34");
                  first.Add("13","45");

                  Hashtable second = new Hashtable();
                  second.Add("14","15");

                  Hashtable merged = new Hashtable(first);
                  foreach(DictionaryEntry e in second)
                  {
                        if (! merged.ContainsKey(e.Key))
                              merged.Add(e.Key,e.Value);
                  }
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now