Search code examples
c#ilist

AND between two list of integer in c#


i need AND between two list of integer in c# foe example if i have

IList<int> list1 = new List<int>(1,2,7,4);

IList<int> list2 = new List<int>(4,2,3,5);

and i need this output List3 = List1 & List2; then list3 items is 2,4;


Solution

  • You can do list1.Intersect(list2).

    Since this function calculates the intersection set, Intersect only returns distinct elements, so {1,2,2}.Intersect({1,2}) => {1,2}.