, List.Remove(), Remove() , , . , . , , List.RemoveAt(), , .
, , intList.LastIndexOf(occurrenceLimit) 8, Remove() 8 , , false . :
intList.Remove(intList.LastIndexOf(occurrenceLimit));
to
intList.RemoveAt(intList.LastIndexOf(occurrenceLimit));
"" , . , , foreach.
, , , LINQ . :
1) occurenceLimit, , , . , intList[i].
2) Remove() RemoveAt().
3) foreach do while . while, , for, ( , foreach). for - originenceLimit, , occurenceLimit - , , .
static void Main(string[] args)
{
int[] arr = new int[] { 1, 2, 1, 4, 5, 1, 2, 2, 2 };
int occurrenceLimit = 2;
var intList = arr.ToList();
for (int i = 0; i < intList.Count; i++)
{
int occursintegerOccurrence = intList.Count(n => n == intList[i]);
while (occursintegerOccurrence > occurrenceLimit)
{
for (var ii = 0; ii < occursintegerOccurrence - occurrenceLimit; ii++)
{
var index = intList.LastIndexOf(intList[i]);
intList.RemoveAt(index);
}
occursintegerOccurrence = intList.Count(n => n == intList[i]);
}
}
foreach (var item in intList)
{
Console.Write(item + " ");
}
Console.WriteLine(Environment.NewLine + "Done");
Console.ReadLine();
}