1. 程式人生 > >兩個集合的交集,並集,差集如何計算

兩個集合的交集,並集,差集如何計算

string[] arr1 = { "445345", "fsdfsdf", "423423", "123" };
            string[] arr2 = { "123", "Fsdfsdf", "3243", "324234" };
            var query1 = arr1.Union(arr2);//兩個集合的並集
            var i = 1;
            Console.WriteLine("輸出並集");
            foreach (string item in query1)
            {
                Console.WriteLine(i + ":" + item);
                i++;
            }

            var query2 = arr1.Intersect(arr2);
            i = 1;
            Console.WriteLine("輸出交集");
            foreach (string item in query2)
            {
                Console.WriteLine(i + ":" + item);
                i++;
            }

            var query3 = arr1.Except(arr2);
            i = 1;
            Console.WriteLine("arr1有的,arr2沒有的");
            foreach (string item in query3)
            {
                Console.WriteLine(i + ":" + item);
                i++;
            }

            var query4 = arr2.Except(arr1);
            i = 1;
            Console.WriteLine("arr2有的,arr1沒有的");

            foreach (string item in query4)
            {
                Console.WriteLine(i + ":" + item);
                i++;
            }