2

I have one outer List BigList and then in my switch statements I have a bunch of other List smallerList variables. When I get to each of these cases in my switch I want to add those to the BigList But I also don't want to add repeated ones. How do we do that?

private List<string> MyMethod()
{
  List<string> BigList = null;
  for each( string name in MyListOfStringsThatComesIn)
  {
     tName = name;
     switch(tName)
     {
         case "dsdds":
            // List<string> smallerList;
            // add it to BigList
         case "fdfdf":
            // List<string> smallerList2;
            // add it to BigList
         case "vbbughb":
            // List<string> smallerList3;
            // add it to BigList

3 Answers3

4

If duplicates aren't allowed i would use a HashSet<string> in the first place:

HashSet<string> bigSet = new HashSet<string>();
// add strings ...

If you want to add the whole List<string> into the set you can either use bigSet.Add in a loop or HashSet.UnionWith:

case "dsdds":
   bigSet.UnionWith(smallerList);

If you need to return a list you can use

return new List<string>(bigSet);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Looks like HashSet doesn't have a AddRange method, so How am I supposed to add the small list to the biglist? –  Jun 02 '14 at 14:16
  • 1
    I believe that OP is trying to add smaller lists to `BigList` (or `bigSet`). You can use `UnionWith` (http://stackoverflow.com/questions/15267034/is-there-an-addrange-equivalent-for-a-hashset-in-c-sharp) – allonhadaya Jun 02 '14 at 14:16
  • @DevWannaBe, you might be interested in ^ – allonhadaya Jun 02 '14 at 14:16
  • 1
    @DevWannaBe: i have edited my answer, you can use a loop or `bigSet.UnionWith(smallerList);` – Tim Schmelter Jun 02 '14 at 14:22
1

To create a new list based on the unique values of another one :

List<string> BigList = MyListOfStringsThatComesIn.Distinct().ToList();

To add new unique values from another list :

//assume the BigList contains something already...
BigList.AddRange(BigList.Except(MyListOfStringsThatComesIn));
Xiaoy312
  • 14,292
  • 1
  • 32
  • 44
  • But that's not _adding_, it's _replacing_ the list of strings. – DonBoitnott Jun 02 '14 at 14:14
  • Base on the example that might be good enough since the list is just created prior to the adding. – rae1 Jun 02 '14 at 14:15
  • @DonBoitnott the big list is initially empty in the current context. Adding or Creating make no difference this is case. – Xiaoy312 Jun 02 '14 at 14:15
  • While that is true, I would avoid making assumptions, especially against already bad pseudo-code. Should make for more complete and useful answers. – DonBoitnott Jun 02 '14 at 14:17
  • @DonBoitnott repeated `!Contains` and `Add` will be rather inefficient in this case. And not to mentions, it would break the DRY principle. – Xiaoy312 Jun 02 '14 at 14:21
1

Well, there's probably a more efficient way to do what you want, but based on what you have shown, you can either:

Look for strings that don;t exist in the parent list:

BigList.AddRange(smallerList.Except(BigList));

or just add them all (allowing duplicates) and call Distinct at the end:

BigList.AddRange(smallerList);
...
///add other lists


BigList = BigList.Distinct().ToList();

Also, you should probably intialize your list to an empty list rather then null:

List<string> BigList = new List<string>();
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • I was thinking about calling Distinct at the end too :) Wasn't sure if it will actually work! –  Jun 02 '14 at 14:16