locked
How compare 2 string arrays with duplicate records RRS feed

  • Question

  • User339833461 posted

    Hello Everyone,

    I have 2 string arrays for example

    string[] a = {"The","Big", "Ant", "The"};
    string[] b = {"Big","Ant","Ran", "The"};

    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:38 AM

All replies

  • User-943250815 posted

    Try "Contains"

        string[] a = { "The", "Big", "Ant", "The" };
        string[] b = { "Big", "Ant", "Ran", "The" };
    
        var c = (from item in a
                 where b.Contains(item)
                 select item).ToArray(); // Get all "a" itens exist in "b"
    
        var d = (from item in a
                 where b.Contains(item)
                 select item).Distinct().ToArray(); // Get all "a" itens exist on "b" - distinct
    

    Monday, March 16, 2020 1:01 PM