Skip to content

Instantly share code, notes, and snippets.

@kakkun61
Created March 15, 2017 11:22
Show Gist options
  • Save kakkun61/6496693cd6fc79eb92e4c80270f552a2 to your computer and use it in GitHub Desktop.
Save kakkun61/6496693cd6fc79eb92e4c80270f552a2 to your computer and use it in GitHub Desktop.
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