How to Merge string Arrays and keep distinct values using LINQ in C# ?

Do you want to merge string arrays and yet want to keep the distinct values in C# ? . One of the option to do this is using the Union extension method.

Below is a sample code snippet demonstrating how to do it.

How to Merge string Arrays and keep distinct values using LINQ in C# ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace GinktageConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            MergeArray();
            Console.ReadLine();
        }
        public static void MergeArray()
        {
            string[] movies1 = new string[] { "Kaththi", "Yennai Arindhaal", "Lingaa" };
            string[] movies2 = new string[] { "Kaththi", "Jilla", "Veeram" };
            string[] result = movies1.Union(movies2).ToArray();
            foreach (var item in result)
                Console.WriteLine(item);
        }
    }
}

image

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Reply


x

Interesting BlogPosts to read

How to perform Shallow Copy in C# ? by @abundantcode
To perform Shallow copy in C# , you can use the this.MemberwiseClone() method. How to perform Shall...
How to add entries to ResourceDirectory in WPF using C# ?
Below is a sample code snippet demonstrating how the entries can be added to the ResourceDirectory i...
How to convert Array to List in C#?
Want to convert array of strings to List of strings in C# ? . Below is a sample code to illustrate t...