Skip to content

Instantly share code, notes, and snippets.

@vivainio
Last active December 1, 2016 12:49
Show Gist options
  • Select an option

  • Save vivainio/01ede8a6e2b634cf4da6970f68427181 to your computer and use it in GitHub Desktop.

Select an option

Save vivainio/01ede8a6e2b634cf4da6970f68427181 to your computer and use it in GitHub Desktop.
public class Collector<TKey>
{
private HashSet<TKey> seen = new HashSet<TKey>();
public IEnumerable<TData> CollectBy<TData>(IEnumerable<TData> input, Func<TData, TKey> extractKey)
{
foreach (var inp in input)
{
var key = extractKey(inp);
if (seen.Contains(key))
continue;
seen.Add(key);
yield return inp;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment