I'd just like to offer a different perspective, for comparative purposes.
Suppose that you are doing this reverse lookup frequently, and you want it to be better than an O(N) operation.
You can achieve that using two dictionaries instead of one. To simplify things in this example, I'll use Microsoft's pre-release MultiValueDictionary
(which you can obtain via NuGet).
This approach yields an O(1) lookup:
using System;
using System.Collections.Generic;
using System.Linq;
namespace Demo
{
internal class Program
{
public static void Main()
{
var names = new Names();
names.Add(1, "Robin");
names.Add(1, "Rahul");
names.Add(1, "Adam");
names.Add(1, "Akhtar");
names.Add(2, "Sun");
names.Add(2, "Mon");
names.Add(2, "Adam");
names.Add(3, "a");
names.Add(3, "a");
names.Add(3, "c");
Console.WriteLine("IDs for Adam:");
foreach (int id in names.IdsOf("Adam"))
Console.WriteLine(id);
}
public sealed class Names
{
readonly MultiValueDictionary<int, string> names = new MultiValueDictionary<int, string>();
readonly MultiValueDictionary<string, int> lookup = new MultiValueDictionary<string, int>();
public void Add(int id, string name)
{
names.Add(id, name);
lookup.Add(name, id);
}
public IEnumerable<int> IdsOf(string name)
{
IReadOnlyCollection<int> result;
if (lookup.TryGetValue(name, out result))
return result;
else
return Enumerable.Empty<int>();
}
public IEnumerable<string> NamesOf(int id)
{
IReadOnlyCollection<string> result;
if (names.TryGetValue(id, out result))
return result;
else
return Enumerable.Empty<string>();
}
}
}
}