1

I have 2 string arrays.I want to remove all the duplicates from a string[] b based on string[] a. I don't want to alter string[] a. I want to remove strings from string[] b that are common in both string[] a and string[] b

for example

a = { "1", "2", "3", "4"};
b = { "3", "4", "5", "6"};

the result i am looking for is

a = { "1", "2", "3", "4"};
b = { "5", "6"};

please help.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Erma
  • 337
  • 1
  • 6
  • 14
  • Does this answer your question? [Remove items of list from another lists with criteria](https://stackoverflow.com/questions/4312437/remove-items-of-list-from-another-lists-with-criteria) – xdtTransform Nov 26 '20 at 12:53
  • And [Remove items from one list in another](https://stackoverflow.com/questions/2745544/remove-items-from-one-list-in-another?noredirect=1&lq=1), [How would you do a “not in” query with LINQ?](https://stackoverflow.com/questions/183791/how-would-you-do-a-not-in-query-with-linq) – xdtTransform Nov 26 '20 at 12:55

2 Answers2

4
b = b.Where(x => !a.contains(x));

Or

b.RemoveAll(x => !a.contains(x));
DTul
  • 646
  • 6
  • 21
1

Another option on top of what's already been answered:

b = b.Except(a).ToArray();

Note that Except() is a method in System.Linq and returns IEnumerable, so in order to reassign the result to b you need to call .ToArray().

devNull
  • 3,849
  • 1
  • 16
  • 16