Get distinct list between two lists in C#

Viewed 44063

I have two lists of strings. How do I get the list of distinct values between them or remove the second list elements from the first list?

List<string> list1 = { "see","you","live"}

List<string> list2 = { "see"}

The result should be {"you","live"}.

4 Answers

This is the good way I find unique....

Unique from two list

        var A = new List<int>() { 1,2,3,4 };
        var B = new List<int>() { 1, 5, 6, 7 };

       var a= A.Except(B).ToList();
        // outputs List<int>(2) { 2,3,4 }
       var b= B.Except(A).ToList();
        // outputs List<int>(2) { 5,6,7 }
       var abint=  B.Intersect(A).ToList();
        // outputs List<int>(2) { 1 }
here is my answer,
find distinct value's in two int list and assign that vlaues to the third int list.


List<int> list1 = new List <int>() { 1, 2, 3, 4, 5, 6 }; 
            List<int> list2 = new List<int>() { 1, 2, 3, 7, 8, 9 };
            List<int> list3 = new List<int>();
            var DifferentList1 = list1.Except(list2).Concat(list2.Except(list1));
            foreach (var item in DifferentList1)
            {
                list3.Add(item);

            }
            foreach (var item in list3)
            {
                Console.WriteLine("Different Item found in lists are{0}",item);
            }
            Console.ReadLine();
Related