0

Hi there. I was hoping I could ask for some advice in regards to a problem I am struggling with.

I have a List with more than a thousand values and there are some duplicates, not exact duplicates but discrepencies based on upper and lower case. so for example I would have

Training and training in the same list or Vision and Values and Vision and values.

So there are various instances where there are minor discrepies based on Case difference.

How could I go about in removing thsese 'excess' values?

Arianule
  • 8,811
  • 45
  • 116
  • 174

3 Answers3

2

Use Linq:

var listWithDups = new List<string>() = {"blah","Blah","etc","etc."};
var listWithoutDups = listWithDups.Distinct(StringComparer.CurrentCultureIgnoreCase).ToList();
Paul Grimshaw
  • 19,894
  • 6
  • 40
  • 59
1

I would add all entries to a Hashset

A Hashset is a collection that stores maximum one of every item added to it. You'd write a "ignore case" equity comparer that you'd pass into the Hashset construcor. Like:

     var set = new Hashset( yourListWithDuplicates, (x,y) => x.Equals(y, 
StringComparison.CurrentCultureIgnoreCase));
mortb
  • 9,361
  • 3
  • 26
  • 44
1

Tried this in LinqPad:

var list = new List<String> { "Hello", "World", "HELLO", "beautiful", "WORLD" };

var l = list.Distinct(StringComparer.CurrentCultureIgnoreCase).ToList();

Console.WriteLine(l);
David Brabant
  • 41,623
  • 16
  • 83
  • 111