Remove duplicates within a List, ignore case and whitespace

Hi, how do I remove duplicate elements within a List of Strings?

Ignore differences in case and whitespace, if only differences are case and/or whitespace, treat as same element and remove these duplicates. Modify the sole remaining element to contain no whitespace, all letters change to upper case

1 Like

Hello @DEATHFISH,

You can do try this, pass List of strings Varibale in For each loop and mention the input as lstVar.Distinct .|

Cheers
@DEATHFISH

1 Like

Thanks @Pradeep_Shiv

Would lstVar.Distinct be able to treat similar elements with whitespace and case differences as the same element?

2 Likes

Can you try and let me know?
@DEATHFISH

1 Like

Hello @DEATHFISH,

It wont treat similar elements with whitespaces and case diff

This is a similar question for this, I don’t know if it will ignore whitespace or ignore case:

To ignore case you can use this:

list.Distinct(
    StringComparer.CurrentCultureIgnoreCase).ToList();

More details about question you can find here:

For whitespace it is more complex since it is not a problem of a distinct function but a form of data so can you clean data before you go into filtering?

1 Like

@DEATHFISH
if I got you right then you would like to get the distincts with trim and changed to uppercase

give a try on yourList.Select(function (x) x.Trim.ToUpper()).Distinct().ToList

2 Likes