Is there a \"nice\" way to eliminate consecutive duplicates of list elements?
Example:
[\"red\"; \"red\"; \"blue\"; \"green
Like this you don't need a new object.
public static void RemoveConsecutiveDuplicates(this List collection)
{
for (int i = 0; i < collection.Count - 1; i++)
{
if (collection[i].Equals(collection[i + 1]))
{
collection.RemoveAt(i);
i--;
}
}
}
var collection = new [] { 2, 7, 7, 7, 2, 6, 4 }.ToList();
collection.RemoveConsecutiveDuplicates();