Dot Net For All

C# Tip: Join or Concat to Merge two Arrays using LINQ

Hello Friends, In C# or any programming language we come across many scenarios where we may want to concatenate or merge two arrays or lists. This can be easily accomplished using LINQ in C#.

Merging two arrays using Concat and Union

LINQ provides us two methods to merge two arrays or Lists in C# and VB.NET

These are Union and Concat. Union removes the duplicate and keeps the unique items in the resulting array while concat merges the two arrays without any removals.

Below is the code examples for the two in C#:

 int[] firstArray = {1,2,3};
            int[] secondArray = {3,4,5,6};

            var output = firstArray.Union(secondArray);
            var concat = firstArray.Concat(secondArray);


            Console.WriteLine("Union Items");
            output.ToList().ForEach(item => Console.WriteLine(item)); // Output: 1,2,3,4,5,6

            Console.WriteLine("Concatenated Items");
            concat.ToList().ForEach(item => Console.WriteLine(item)); //Output: 1,2,3,3,4,5,6

Union determines the uniqueness of element by comparing the elements by their equality comparer.

Lets take an example of the class defined below as Person.

    public class  Person{

        public string FirstName { get; set; }
        public string LastName { get; set; }

        public override Boolean Equals(object obj){
            Person arg = obj as Person;
            if(arg != null){
                return arg.FirstName.Equals(this.FirstName);
            }
            return  false;
        }

        public override int GetHashCode(){
            return this.FirstName.GetHashCode();
        }

        public override string ToString(){
            return this.FirstName + " " + this.LastName;
        }
    }

In the above code the equality of the person class is determined by the FirstName property. If the first name of any two persons are same they are considered equal as per this code.

            Person[] personArray = {new Person() { FirstName = "Vikram", LastName = "Chaudhary" },
             new Person() { FirstName = "Manoj", LastName = "Sharma" }};

           Person[] personSecondArray = {new Person() { FirstName = "Vikram", LastName = "Sharma" },
             new Person() { FirstName = "Manoj", LastName = "Patil" },
             new Person() { FirstName = "Divya", LastName = "Patel" }};

            var output1 = personArray.Union(personSecondArray);
            var concat1 = personArray.Concat(personSecondArray);

            Console.WriteLine("Union Items");
            output1.ToList().ForEach(item => Console.WriteLine(item)); 

            Console.WriteLine("\n Concatenated Items");
            concat1.ToList().ForEach(item => Console.WriteLine(item)); 

Now lets see the output of the above code.

Union and Concatenate in

Conclusion:

This is a simple example to use join or concat to merge to arrays or List to get a single array with result in C#

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview