KeepUpToN
Filters a collection keeping up to n occurences of each value.
- Use
IEnumerable.Distinct()in combination withIEnumerable.ToDictionary()to create a dictionary with an initial count of0for each distinct value indata. - Use
IEnumerable.Where()to filter out occurences after thenth one for each element, using the previously created dictionary.
using System.Collections.Generic;
using System.Linq;
public static partial class _30s
{
public static IEnumerable<T> KeepUpToN<T>(IEnumerable<T> data, int n)
{
var occurences = data.Distinct().ToDictionary(i => i, value => 0);
return data.Where(i => occurences[i]++ < n);
}
}int[] nums = {1, 1, 2, 3, 3, 3, 1};
_30s.KeepUpToN(nums, 2); // {1, 1, 2, 3, 3}