I expanded the answer of @Enigmativity to provide two generic versions as extension methods.
/// <summary> Removes all duplicate values after the first occurrence. </summary>
public static void RemoveDuplicates<T, V> (this Dictionary<T, V> dict)
{
List<V> L = new List<V> ();
int i = 0;
while (i < dict.Count)
{
KeyValuePair<T, V> p = dict.ElementAt (i);
if (!L.Contains (p.Value))
{
L.Add (p.Value);
i++;
}
else
{
dict.Remove (p.Key);
}
}
}
With a dictionary of values (ignoring keys): 0, 1, 2, 3, 5, 1, 2, 4, 5.
Result: 0, 1, 2, 3, 5, 4.
/// <summary> Removes all values which have any duplicates. </summary>
public static void RemoveAllDuplicates<T, V> (this Dictionary<T, V> dict)
{
List<V> L = new List<V> ();
int i = 0;
while (i < dict.Count)
{
KeyValuePair<T, V> p = dict.ElementAt (i);
if (!L.Contains (p.Value))
{
L.Add (p.Value);
i++;
}
else
{
dict.Where (j => Equals (j.Value, p.Value)).ToList ().ForEach (j => dict.Remove (j.Key));
}
}
}
With a dictionary of values (ignoring keys): 0, 1, 2, 3, 5, 1, 2, 4, 5.
Result: 3, 4.
The methods are optimized to prevent multiple executions of the .Where operation (otherwise each duplicate would have n executions of it, where all after the first one is obsolete). Code is tested and working.