【问题标题】:remove duplicates from two string arrays c#从两个字符串数组中删除重复项c#
【发布时间】:2012-11-25 18:17:53
【问题描述】:

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

例如

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

我正在寻找的结果就是

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

【问题讨论】:

  • “2”和“1”发生了什么?有一次出现......他们应该在结果中吗?
  • @poplitea 重复问题不同,它只给出 1 个数组中的重复项,而不是两个不同的数组
  • @nemesv 没有数组 b 将是 a 的更新版本
  • +1 我认为这是一个有趣的问题,无法理解为什么你得到了这么多反对
  • 可能有阴谋获得反转金徽章……哈哈

标签: c# c#-4.0


【解决方案1】:
var final = b.Except(a).ToList();

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      你可以试试这个:-

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

      【讨论】:

        【解决方案4】:
                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());
                            }
                        }
                    }
                 }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-06-09
          • 2012-09-15
          • 1970-01-01
          • 2016-01-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多