简体   繁体   中英

Comparing Element of list A and B and finding elements that are not present in List B?

I Have two List

List<int> A= new List<int>()
{33,50,30,90,1,4,5,6,66,
}; 
and 
List<int> B=new List<int>()
   {50,4,33};

Now I want to find all the elements of List A which are not present in List B

List<int> res = A.Except(B).ToList();
using System.IO;

using System;

using System.Linq;

using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> A= new List<int>(){33,50,30,90,1,4,5,6,66,}; 
        List<int> B=new List<int>(){50,4,33};

        Console.WriteLine("using linq:");

        foreach(int i in A.Except(B).ToList())
        Console.WriteLine(i);

        Console.WriteLine("without using linq:");

        foreach(int i in Except(A,B))
        Console.WriteLine(i);
    }

    private static List<int> Except(List<int> A,List<int>B)
    {
        List<int> c = new List<int>();
        foreach(int i in A)
        {
            if(B.Contains(i))
               continue;
            else
               c.Add(i);
        }
        return c;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM