SortedDictionary.Remove() Method in C#

The SortedDictionary.Remove() method in C# is used to remove the element with the specified key from the SortedDictionary<TKey, TValue>. This method returns a bool value indicating whether the removal was successful.

Syntax

Following is the syntax for the Remove() method −

public bool Remove (TKey key);

Parameters

  • key − The key of the element to remove from the SortedDictionary.

Return Value

The method returns true if the element is successfully found and removed; otherwise, false. This method also returns false if the key is not found in the original SortedDictionary.

Using Remove() with Existing Keys

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
      sortedDict.Add(100, "Mobile");
      sortedDict.Add(200, "Laptop");
      sortedDict.Add(300, "Desktop");
      sortedDict.Add(400, "Speakers");
      sortedDict.Add(500, "Headphone");
      sortedDict.Add(600, "Earphone");
      
      Console.WriteLine("SortedDictionary key-value pairs...");
      foreach(var item in sortedDict) {
         Console.WriteLine("Key = " + item.Key + ", Value = " + item.Value);
      }
      
      Console.WriteLine("\nKey 400 removed from SortedDictionary? = " + sortedDict.Remove(400));
      
      Console.WriteLine("\nSortedDictionary key-value pairs...UPDATED");
      foreach(var item in sortedDict) {
         Console.WriteLine("Key = " + item.Key + ", Value = " + item.Value);
      }
   }
}

The output of the above code is −

SortedDictionary key-value pairs...
Key = 100, Value = Mobile
Key = 200, Value = Laptop
Key = 300, Value = Desktop
Key = 400, Value = Speakers
Key = 500, Value = Headphone
Key = 600, Value = Earphone

Key 400 removed from SortedDictionary? = True

SortedDictionary key-value pairs...UPDATED
Key = 100, Value = Mobile
Key = 200, Value = Laptop
Key = 300, Value = Desktop
Key = 500, Value = Headphone
Key = 600, Value = Earphone

Using Remove() with Non-Existing Keys

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
      sortedDict.Add(1, "Ultrabook");
      sortedDict.Add(2, "Alienware");
      sortedDict.Add(3, "Notebook");
      sortedDict.Add(4, "Connector");
      
      Console.WriteLine("SortedDictionary key-value pairs...");
      foreach(var item in sortedDict) {
         Console.WriteLine("Key = " + item.Key + ", Value = " + item.Value);
      }
      
      // Try to remove existing key
      Console.WriteLine("\nKey 2 removed? = " + sortedDict.Remove(2));
      
      // Try to remove non-existing key
      Console.WriteLine("Key 99 removed? = " + sortedDict.Remove(99));
      
      Console.WriteLine("\nSortedDictionary key-value pairs...UPDATED");
      foreach(var item in sortedDict) {
         Console.WriteLine("Key = " + item.Key + ", Value = " + item.Value);
      }
   }
}

The output of the above code is −

SortedDictionary key-value pairs...
Key = 1, Value = Ultrabook
Key = 2, Value = Alienware
Key = 3, Value = Notebook
Key = 4, Value = Connector

Key 2 removed? = True
Key 99 removed? = False

SortedDictionary key-value pairs...UPDATED
Key = 1, Value = Ultrabook
Key = 3, Value = Notebook
Key = 4, Value = Connector

Key Rules

  • The Remove() method returns true if the key exists and is successfully removed.
  • The method returns false if the key is not found in the SortedDictionary.
  • After removal, the SortedDictionary maintains its sorted order based on the keys.
  • The method has O(log n) time complexity due to the underlying balanced binary search tree structure.

Conclusion

The SortedDictionary.Remove() method provides an efficient way to remove key-value pairs from a SortedDictionary. It returns a boolean value to indicate the success or failure of the removal operation, making it easy to handle cases where the key might not exist.

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

184 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements