-1

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

例如

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

我正在寻找的结果就是

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

4 回答 4

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

enter image description here

于 2012-11-25T18:22:48.083 回答
7

Enumerable.Except produces set difference of two sequences

var c = b.Except(a).ToArray(); // gives you { "5", "6" }
于 2012-11-25T18:22:44.503 回答
2

YOu can try using this:-

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

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

or try this:-

 var c = b.Except(a).ToArray();
于 2012-11-25T18:20:07.237 回答
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());
                    }
                }
            }
         }
于 2016-10-27T21:35:01.727 回答