-1

How can i effectively remove duplicates from two string arrays? I want to remove all the duplicates from a string[] a that are in a separate string[] b

for example

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

the result i am looking for would just be

c = { "5", "6"};
pyCthon
  • 11,746
  • 20
  • 73
  • 135

4 Answers4

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

enter image description here

L.B
  • 114,136
  • 19
  • 178
  • 224
7

Enumerable.Except produces set difference of two sequences

var c = b.Except(a).ToArray(); // gives you { "5", "6" }
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
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();
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
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());
                    }
                }
            }
         }
Mustafa Ekici
  • 7,263
  • 9
  • 55
  • 75