Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reducing the number of repeating same strings or integers to 1

Initial:

 list = { null, 1, 2, null, null, null, 4, 5, 6 }; 

Expectatitons:

 list = { null, 1, 2, null, 4, 5, 6 };

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading