3

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

0

2 Answers 2

7

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

6 Comments

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

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.