2012-11-25 135 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()); 
        } 
       } 
      } 
     }