public CHashSet <T> Intersect(CHashSet <T> other) { var result = new CHashSet <T>(); foreach (var item in this.values) { foreach (var otherItem in other.values) { if (item.Key == otherItem.Key) { result.Add(item.Value); } } } return(result); }
static void Main() { var hashSet = new CHashSet <int>(); hashSet.Add(1); hashSet.Add(1); hashSet.Add(2); hashSet.Add(1); foreach (var item in hashSet) { Console.Write(item + " "); } Console.WriteLine(); hashSet.Remove(1); foreach (var item in hashSet) { Console.Write(item + " "); } Console.WriteLine(); var secondHashSet = new CHashSet <int>(); secondHashSet.Add(3); secondHashSet.Add(4); secondHashSet.Add(2); var unionTest = hashSet.Union(secondHashSet); foreach (var item in unionTest) { Console.Write(item + " "); } Console.WriteLine(); var intersectTest = hashSet.Intersect(secondHashSet); foreach (var item in intersectTest) { Console.Write(item + " "); } }
public CHashSet <T> Union(CHashSet <T> other) { var result = new CHashSet <T>(); foreach (var item in this.values) { result.Add(item.Value); } foreach (var item in other.values) { if (!result.values.Contains(item.Key)) { result.Add(item.Value); } } return(result); }