Looking for structure that contains the benefits of a stack but the ability to only contain one item that matches a key.
For example Data comes in from various clients,
It seems like you want something that would be called HashStack
.
T
would need to implement IEquatable
or override Equals
and GetHashCode
, or you can accept an IEqualityComparer
.
If you don't want to overcomplicate things, you'll need to implement IList
with Push
method. The Add
method will be called by Push
and it'll need to evaluate if the item to be added is already in the stack by either calling item's GetHashCode
/Equals
or you can also maintain an internal HashSet
to optimize this check that has already implemented equality check (HashSet
will return false
if the item is already in the set...).
Items would need to be also stored in an internal List
to being able to get last item by insertion order.