Initial:
list = { null, 1, 2, null, null, null, 4, 5, 6 };
Expectatitons:
list = { null, 1, 2, null, 4, 5, 6 };
>Solution :
If list is an obsolete ArrayList or List<oblect> you can try a simple for loop:
var list = new ArrayList { null, 1, 2, null, null, null, 4, 5, 6 };
...
int index = 0;
// Remove consequent null's
for (int i = 1; i < list.Count; ++i)
if (list[i] != null || list[index] != null)
list[index++] = list[i];
list.RemoveRange(index, list.Count - index);
To get rid of consequent duplicates
int index = 0;
for (int i = 1; i < list.Count; ++i)
if (list[i] != list[index])
list[index++] = list[i];
list.RemoveRange(index, list.Count - index);