2012-11-25 98 views
-2

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

例如

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

重复的我期待的结果也只是

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

什么与 “2” 和 “1” 发生的差异?有一次出现...他们是否应该在结果中? – nemesv

+1

@poplitea重复的问题是不同的,它只给出1个数组中的重复不是两个不同的数组 – pyCthon

+0

@nemesv没有数组b会是 – pyCthon

回答

6

Enumerable.Except产生设置两个序列

var c = b.Except(a).ToArray(); // gives you { "5", "6" } 
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(); 
13
var final = b.Except(a).ToList(); 

enter image description here

+0

真棒插图! – MUG4N

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()); 
        } 
       } 
      } 
     }