Created
March 15, 2017 11:22
-
-
Save kakkun61/6496693cd6fc79eb92e4c80270f552a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
class CounterHashSet<T> | |
{ | |
Dictionary<T, int> container; | |
public CounterHashSet() | |
{ | |
container = new Dictionary<T, int>(); | |
} | |
public bool Add(T element) | |
{ | |
if (container.ContainsKey(element)) | |
{ | |
container[element]++; | |
return true; | |
} | |
container[element] = 1; | |
return true; | |
} | |
public bool Remove(T element) | |
{ | |
if (!container.ContainsKey(element)) | |
{ | |
return false; | |
} | |
if (container[element] == 1) | |
{ | |
container.Remove(element); | |
return true; | |
} | |
container[element]--; | |
return true; | |
} | |
public HashSet<T> ToHashSet() | |
{ | |
return new HashSet<T>(container.Keys); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment