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
1
  • Make BigList a HashSet, that way it won't have duplicates. Commented Jun 2, 2014 at 14:17

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

4 Comments

Looks like HashSet doesn't have a AddRange method, so How am I supposed to add the small list to the biglist?
I believe that OP is trying to add smaller lists to BigList (or bigSet). You can use UnionWith (stackoverflow.com/questions/15267034/…)
@DevWannaBe, you might be interested in ^
@DevWannaBe: i have edited my answer, you can use a loop or bigSet.UnionWith(smallerList);
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));

5 Comments

But that's not adding, it's replacing the list of strings.
Base on the example that might be good enough since the list is just created prior to the adding.
@DonBoitnott the big list is initially empty in the current context. Adding or Creating make no difference this is case.
While that is true, I would avoid making assumptions, especially against already bad pseudo-code. Should make for more complete and useful answers.
@DonBoitnott repeated !Contains and Add will be rather inefficient in this case. And not to mentions, it would break the DRY principle.
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>();

1 Comment

I was thinking about calling Distinct at the end too :) Wasn't sure if it will actually work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.