C# Program to Merge Two Hashtable Collections

The Hashtable collection in C# stores key-value pairs where each element is a DictionaryEntry object. The key is unique and non-null, used to access elements in the hashtable. Values can be null or duplicated, but the combination of key-value pairs must be unique.

Since the Hashtable class doesn't provide a built-in merge method, we need to implement our own approach by iterating through one hashtable and adding its elements to another while checking for duplicate keys.

Syntax

Following is the syntax for creating and populating a Hashtable

Hashtable hashtableName = new Hashtable();
hashtableName.Add(key, value);

Following is the syntax for checking if a key exists before adding

if (!hashtable1.ContainsKey(key)) {
   hashtable1.Add(key, value);
}

Merging Algorithm

The approach to merge two hashtables involves the following steps

  • Create and populate two hashtable objects

  • Iterate through the second hashtable using foreach loop

  • For each key-value pair, check if the key exists in the first hashtable using ContainsKey()

  • If the key doesn't exist, add the key-value pair to the first hashtable

Hashtable Merge Process Hashtable 1 {1: "One"} {10: "Ten"} {100: "Hundred"} Hashtable 2 {1: "Ones"} {2: "Two"} {3: "Three"} Merge ? Result: {1:"One", 10:"Ten", 100:"Hundred", 2:"Two", 3:"Three"}

Using Non-Duplicate Keys

Example

using System;
using System.Collections;

class MergeHashtables {
   static public void Main() {
      Hashtable indianNumberSystem = new Hashtable();
      indianNumberSystem.Add(1, "Ones");
      indianNumberSystem.Add(10, "Tens");
      indianNumberSystem.Add(100, "Hundred");
      indianNumberSystem.Add(1000, "Thousand");
      
      Console.WriteLine("Contents of indianNumberSystem hashtable:");
      foreach(DictionaryEntry ele1 in indianNumberSystem) {
         Console.WriteLine("{0} ({1})", ele1.Key, ele1.Value);
      }
      
      Hashtable langCodes = new Hashtable();
      langCodes.Add("C++", "CPlusPlus");
      langCodes.Add("C#", "CSharp");
      langCodes.Add("Java", "Java");
      langCodes.Add("PL", "Perl");
      
      Console.WriteLine("\nContents of langCodes Hashtable:");
      foreach(DictionaryEntry ele1 in langCodes) {
         Console.WriteLine("{0} ({1})", ele1.Key, ele1.Value);
      }
      
      foreach (DictionaryEntry entry in langCodes) {
         if(!indianNumberSystem.ContainsKey(entry.Key)) {
            indianNumberSystem.Add(entry.Key, entry.Value);
         }
      }
      
      Console.WriteLine("\nMerged hashtable contents:");
      foreach(DictionaryEntry ele1 in indianNumberSystem) {
         Console.WriteLine("{0} ({1})", ele1.Key, ele1.Value);
      }
   }
}

The output of the above code is

Contents of indianNumberSystem hashtable:
1000 (Thousand)
10 (Tens)
100 (Hundred)
1 (Ones)

Contents of langCodes Hashtable:
C++ (CPlusPlus)
C# (CSharp)
Java (Java)
PL (Perl)

Merged hashtable contents:
100 (Hundred)
1000 (Thousand)
PL (Perl)
10 (Tens)
C# (CSharp)
Java (Java)
C++ (CPlusPlus)
1 (Ones)

Using Duplicate Keys

Example

using System;
using System.Collections;

class MergeWithDuplicates {
   static public void Main() {
      Hashtable indianNumberSystem = new Hashtable();
      indianNumberSystem.Add(1, "Ones");
      indianNumberSystem.Add(10, "Tens");
      indianNumberSystem.Add(100, "Hundred");
      indianNumberSystem.Add(1000, "Thousand");
      
      Console.WriteLine("Contents of indianNumberSystem hashtable:");
      foreach(DictionaryEntry ele1 in indianNumberSystem) {
         Console.WriteLine("{0} ({1})", ele1.Key, ele1.Value);
      }
      
      Hashtable numberNames = new Hashtable();
      numberNames.Add(1, "One");
      numberNames.Add(2, "Two");
      numberNames.Add(3, "Three");
      numberNames.Add(4, "Four");
      
      Console.WriteLine("\nContents of numberNames Hashtable:");
      foreach(DictionaryEntry ele1 in numberNames) {
         Console.WriteLine("{0} ({1})", ele1.Key, ele1.Value);
      }
      
      foreach (DictionaryEntry entry in numberNames) {
         if(!indianNumberSystem.ContainsKey(entry.Key)) {
            indianNumberSystem.Add(entry.Key, entry.Value);
         }
      }
      
      Console.WriteLine("\nMerged hashtable contents:");
      foreach(DictionaryEntry ele1 in indianNumberSystem) {
         Console.WriteLine("{0} ({1})", ele1.Key, ele1.Value);
      }
   }
}

The output of the above code is

Contents of indianNumberSystem hashtable:
1000 (Thousand)
10 (Tens)
100 (Hundred)
1 (Ones)

Contents of numberNames Hashtable:
4 (Four)
3 (Three)
2 (Two)
1 (One)

Merged hashtable contents:
100 (Hundred)
1000 (Thousand)
10 (Tens)
4 (Four)
3 (Three)
2 (Two)
1 (Ones)

Key Points

Aspect Behavior
Duplicate Keys Existing keys are preserved, duplicates from second hashtable are ignored
Order Elements appear in hash code order, not insertion order
Performance O(n) where n is the size of the second hashtable
Memory First hashtable grows automatically to accommodate new elements

Conclusion

Merging two Hashtable collections in C# requires manually iterating through one hashtable and adding its elements to another while checking for duplicate keys using ContainsKey(). The original values are preserved when duplicate keys are encountered, ensuring data integrity during the merge process.

Updated on: 2026-03-17T07:04:36+05:30

765 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements