Get Common value from two array

Write a program to get unique value from two array?


For Example:

Input:
Arr1= {“Mumabi”,”Pune”,”Delhi”,”Nagpur”}
Arr2= {“Kolkata”,”Patana”,”Pune”,” Mumabi”}

Output:
Arr = {“Mumabi”,”Pune”}

Solution:

using System;
using System.Linq;

namespace ArrayOpearation
{
    class Program
    {
        static void Main()
        {

            var arr1 = new[] { "Mumabi", "Pune", "Delhi", "Nagpur" };
            var arr2 = new[] { "Kolkata", "Patana", "Pune", "Mumabi" };

            var result = arr1.Where(x => arr2.Contains(x)).ToArray();
            Console.WriteLine($"Common value from both array\n{string.Join(", ", result)}");
            Console.ReadLine();
        }
    }
}

Leave a Reply

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