none
How compare 2 string arrays with duplicate records RRS feed

  • Question

  • I have 2 string arrays for example

    string[] a = {"The","Big", "Ant", "The"};
    string[] b = {"Big","Ant","Ran"};
    here i am comparing 2 string arrays(a may contains duplicate elements as well). How to get not matched elements while comparing a and b in b(with duplicates as well). in linq 'Except' i am not getting duplicate records. I need to get duplicates as well.
    b = a.Except(b).ToArray();

    Thanks

    Monday, March 16, 2020 7:35 AM

Answers

  • Hi,

    Thank you for posting here.

    An original method, but can solve the problem.

            static void Main(string[] args)
            {
    
                string[] a = { "The", "Big", "Ant", "The" };
                string[] b = { "Big", "Ant", "Ran" };
    
                List<string> result = new List<string>();
                foreach (var item in a)
                {
                    if (!b.Contains(item))
                    {
                        result.Add(item);
                    }
                }
                foreach (var item in b)
                {
                    if (!a.Contains(item))
                    {
                        result.Add(item);
                    }
                }
                Console.ReadKey();
            }

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 16, 2020 9:44 AM

All replies

  • You can use Union method for that:

    var result = a.Union(b);

    The above code will output:

    The
    Big
    Ant
    Ran

     here is complete sample program :

    using System;
    using System.Linq;
    
    public class Program
    {
    	public static void Main()
    	{
    		string[] a = {"The", "Big", "Ant", "The"};
    		string[] b = {"Big", "Ant", "Ran"};
    
    		var result = a.Union(b);
    
    		foreach (var item in result)
    			Console.WriteLine(item);
    	}
    }

    See working DEMO Fiddle


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites




    Monday, March 16, 2020 7:59 AM
  • hi i need O/p like below 


    var res = {"The","The", "Ran"};
    Monday, March 16, 2020 9:05 AM
  • Hi,

    Thank you for posting here.

    An original method, but can solve the problem.

            static void Main(string[] args)
            {
    
                string[] a = { "The", "Big", "Ant", "The" };
                string[] b = { "Big", "Ant", "Ran" };
    
                List<string> result = new List<string>();
                foreach (var item in a)
                {
                    if (!b.Contains(item))
                    {
                        result.Add(item);
                    }
                }
                foreach (var item in b)
                {
                    if (!a.Contains(item))
                    {
                        result.Add(item);
                    }
                }
                Console.ReadKey();
            }

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 16, 2020 9:44 AM
  • You could concat the arrays, than do a group by and filter the items that occur more than once.

     static void Main(string[] args)
            {
                string[] a = { "The", "Big", "Ant", "The" };
                string[] b = { "Big", "Ant", "Ran" };
    
                IEnumerable<string> duplicates = a.Concat(b).GroupBy(x => x).Where(g => g.Count() > 1).Select(g => g.Key);
    
                foreach (string x in duplicates)
                {
                    Console.WriteLine(x);
                }
                Console.ReadLine();
            }


    Hope this helps,

    Here to learn and share. Please tell if an answer was helpful or not at all. This adds value to the answers and enables me to learn more.

    About me

    Monday, March 16, 2020 10:06 AM