日期:2014-05-20  浏览次数:20535 次

数组的求解
有二个数组
string[] a ={不错,好的,good,ok,9,15,od};
string[] b ={真不错,好的,good,ok,15,odd,look,dddd};

如何求出二个数组不同的部份。
1:打印b比a多的部份
2:打印a中不包含在b中的部分。

希望能有注释,效率要高一点的算法。

------解决方案--------------------
1.b比a多的部份 
b.Where(item => a.Contains(item) == false).ToList()


2.a中不包含在b中的部分。
a.Where(item => b.Contains(item) == false).ToList()

------解决方案--------------------
C# code
public static void GetA1() 
        {
            string[] a = { "不错", "好的", "good", "ok", "9", "15", "od" };
            string[] b = { "真不错", "好的", "good", "ok", "15", "odd", "look", "dddd" };
            string result1=string.Empty;
            string result2=string.Empty;
            foreach (string str1 in b) 
            {
                if (!a.Contains(str1))
                    result1 += str1 + "|";
            }
            foreach (string str1 in a) 
            {
                if (!b.Contains(str1))
                    result2 += str1+"|";
            }
            Console.WriteLine(result1);
            Console.WriteLine(result2);
        }

------解决方案--------------------
C# code
public static void GetA1() 
        {
            string[] a = { "不错", "好的", "good", "ok", "9", "15", "od" };
            string[] b = { "真不错", "好的", "good", "ok", "15", "odd", "look", "dddd" };
            var list1 = b.Where(item => !a.Contains(item)).ToList();
            var list2 = a.Where(item => !b.Contains(item)).ToList();
            foreach (string result1 in list1)
                Console.WriteLine(result1);
            foreach (string result2 in list2)
                Console.WriteLine(result2);
        }