从两个字符串数组c#中删除重复项

时间:2012-11-25 18:17:53

标签: c# c#-4.0

如何有效地从两个字符串数组中删除重复项?我想删除string[] a

中单独的string[] b的所有重复项

例如

a = { "1", "2", "3", "4"};
b = { "3", "4", "5", "6"};

我正在寻找的结果只是

c = { "5", "6"};

4 个答案:

答案 0 :(得分:14)

var final = b.Except(a).ToList();

enter image description here

答案 1 :(得分:6)

Enumerable.Except产生两个序列的集合差异

var c = b.Except(a).ToArray(); // gives you { "5", "6" }

答案 2 :(得分:2)

你可以尝试使用它: -

 List<string[]> moves = new List<string[]>();
 string[] newmove = { piece, axis.ToString(), direction.ToString() };
 moves.Add(newmove);
 moves.Add(newmove);

 moves = moves.Distinct().ToList();

或试试这个: -

 var c = b.Except(a).ToArray();

答案 3 :(得分:0)

        static void Main(string[] args)
        {        
             Foo(m.Length-1);
        }
        static string m = "1234"; 
        static string c = "3456"; 
        static int ctr = 0;
        static void Foo(int arg)
        {
            if (arg >= 0)
            {
                Foo(arg - 1);
                Console.Write(m[arg].ToString());
                for (int i = ctr; i <  m.Length; i++)
                {
                    if (c[i].ToString() == m[arg].ToString())
                    {
                        ctr++;
                        break;
                    }
                }  
                if(arg==m.Length-1)
                {
                    for (int i = ctr; i <m.Length; i++)
                    {
                        Console.Write(c[i].ToString());
                    }
                }
            }
         }