Compare Two Arrays Of Different Lengths and Show Differences

前端 未结 4 607
一向
一向 2021-01-17 17:13

Problem:
I have two arrays that can possibly be different lengths. I need to iterate through both arrays and find similarities, additions, and deletions.

4条回答
  •  伪装坚强ぢ
    2021-01-17 17:59

    You can use Except and Intersect ...

    var Foo_Old = new[] { "test1", "test2", "test3" }; 
    var Foo_New = new[] { "test1", "test2", "test4", "test5" };
    
    var diff = Foo_New.Except( Foo_Old );
    var inter = Foo_New.Intersect( Foo_Old );
    var rem = Foo_Old.Except(Foo_New);
    
    foreach (var s in diff)
    {
        Console.WriteLine("Added " + s);
    }
    
    foreach (var s in inter)
    {
        Console.WriteLine("Same " + s);
    }
    
    foreach (var s in rem)
    {
        Console.WriteLine("Removed " + s);
    }
    

提交回复
热议问题