C#
dictionary
bidirectional
data structures
programming

Bidirectional 1 to 1 Dictionary in C

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

In software development, dictionaries (or maps) are often used to facilitate fast lookups between keys and values. Sometimes, applications require mapping in both directions, which a typical dictionary does not naturally support because lookups inherently favor one direction - from a key to a value. This article explores how a Bidirectional 1-to-1 Dictionary can be implemented in C#, providing an efficient way to access elements in both directions while ensuring bijective relationships.

Concept of a Bidirectional Dictionary

A bidirectional dictionary ensures a one-to-one mapping between two sets of data, allowing for lookup in either direction without the need for duplicate data structures. This means that given a key, you can find a corresponding value, and given a value, you can find its associated key.

Key Characteristics:

  • Bijective Mapping: Every key has a unique value, and every value has a unique key.
  • Constant Time Complexity: Both key-to-value and value-to-key lookups should ideally have O(1) access time.
  • Synchronous Update: Changes in one direction should reflect immediately in the other direction.

Implementation in C#

Primary Data Structures

To implement a bidirectional dictionary in C#, we can utilize two dictionaries: one for the key-to-value mapping and another for the value-to-key mapping. Below is an example implementation:

  • Add(T1 key, T2 value): This method establishes a mapping between a key and a value in both directions while ensuring no duplicates exist.
  • GetValue(T1 key) and GetKey(T2 value): Both methods retrieve elements in constant time, enabling efficient lookups.
  • RemoveByKey(T1 key) and RemoveByValue(T2 value): These methods remove the mapping in both dictionaries, keeping data consistent.
  • Count: This property returns the number of elements in the dictionary, mirroring both maps.
  • Text Processing: Mapping between words and identifiers or codes.
  • Systems with Inverse Relationships: Associating foreign language words with their native translations.
  • Data Synchronization: Keeping corresponding data elements in sync.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.