-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"};
5
  • 3
    What happened with "2" and "1"? There are appear once... Should they be in the result? Commented Nov 25, 2012 at 18:20
  • 1
    @poplitea that duplicate question is different, it only gives the duplicates in 1 array not two different arrays Commented Nov 25, 2012 at 18:30
  • @nemesv no array b would be an updated version of a Commented Nov 25, 2012 at 18:35
  • 1
    +1 I think this is an interesting question and cannot understand why you got so much downvotes Commented Nov 25, 2012 at 19:18
  • There might be a conspiracy to get the reversal gold badge... lol Commented Nov 28, 2012 at 17:13

4 Answers 4

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

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

7

Enumerable.Except produces set difference of two sequences

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

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.