views:

51

answers:

2

I have two array lists

  dim Colors1 = New ArrayList
  Colors1.Add("Blue")
  Colors1.Add("Red")
  Colors1.Add("Yellow")
  Colors1.Add("Green")
  Colors1.Add("Purple")

  dim Colors2 = New ArrayList
  Colors2.Add("Blue")
  Colors2.Add("Green")
  Colors2.Add("Yellow")

I would like to find out which colors are missing from Colors2 that are found in Colors1

+3  A: 

Look at using Except method. "This method returns those elements in first that do not appear in second. It does not also return those elements in second that do not appear in first."

So you can just put colors 2 as the first argument and colors1 as the second.

EDIT: I meant you can put colors 1 first and colors 2 as the second.

EDIT2: (per Sean)

var missingFrom2 = colors1.Except(colors2);
spinon
+1 Never noticed this before. (Note: requires .NET 3.5 or greater)
egrunin
@egruni Yeah good point. Thanks for mentioning that. It is part of Linq extension methods.
spinon
I'll just add since the answer could be confusing, you would write something like 'var missingFrom2 = colors1.Except(colors2);'
Sean Copenhaver
@sean Thanks for the example. Anytime we have an example it always helps.
spinon
+1 for an anwer that *would* work if I wasn't running .NET version 2.0.50727.3053
zeroef
A: 

Just for completeness, I'll add the old-fashioned way.

List<string> result = new List<string>();

foreach (string s in Colors1)
    if (Colors2.Contains(s) == false)
        result.add(s);

// now result has the missing colors
egrunin