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

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...