1

I am trying to write a function that will take two string array and concat them but will escape duplicate array. For instance, in below two arrays cat is common. So I need to take cat only one time

input array1 = ["dog", "cat", "rat"] input array2 = ["fat", "cat", "bat"] output array = ["dog", "cat", "rat", "fat", "bat"]

Any guidance/ solution with built-in function or without any built-in function will help. TIA

1
  • 1
    LINQ Union is what you need. Commented Apr 24, 2017 at 4:02

3 Answers 3

5

Use Union

string[] output = array1.Union(array2).ToArray();

WORKING FIDDLE

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

Comments

2

You can use Union or Distinct.

var output = array1.Union(array2).ToArray();
// or
var output = array1.Concat(array2).Distinct().ToArray();

Comments

2
var array1 = new[] { "dog", "cat", "rat" };
var array2 = new[] { "fat", "cat", "bat"};
var output = array1.Union(array2).ToArray();

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.