Compare two lines of ArrayLists

I have a list of arrays

  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 know what colors are missing in Colors2 which are in Colors1

+3
source share
2 answers

Look at using the Except Method . "This method first returns those elements that are not displayed in the second. It also does not return those elements in the second that are not displayed in the first place."

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

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

EDIT2: (per Sean)

var missingFrom2 = colors1.Except(colors2);
+6
source

, .

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
+1

Source: https://habr.com/ru/post/1768503/


All Articles