当前位置: 代码迷 >> ASP.NET >> 数组的求解,该如何处理
  详细解决方案

数组的求解,该如何处理

热度:4678   发布时间:2013-02-26 00:00:00.0
数组的求解
有二个数组
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);        }
  相关解决方案