Question

I would like to compare 2 lists of arrays. Let's take for instance this example:

 List<int[]> list1 = new List<int[]>() { new int[4] { 1, 2, 3, 4 }, new int[4] { 1, 2, 3, 5 } };
 List<int[]> list2 = new List<int[]>() { new int[2] { 1, 2 }, new int[2] { 3, 4 }, new int[2] { 3, 5 } };

I would like to know for each element in list1 to calculate for every element in list 2 how many common elements they have.

Ex. 1,2,3,4 compared with 1,2 will result to 2 matching elements. 1,2,3,4 compared with 3,5 will result to 1 matching element.

This is not duplicated as I don't wish to compare regular lists. I wish to see for each record in list1 how many items from list2 contain how many common items.

Was it helpful?

Solution 3

You'd do something like this:

var results = 
    from x in list1.Select((array, index) => new { array, index })
    from y in list2.Select((array, index) => new { array, index })
    select new 
    {
        list1_index = x.index,
        list2_index = y.index,
        count = x.array.Intersect(y.array).Count()
    };

foreach(var r in results)
{
    Console.WriteLine("({0}, {1}) have {2} item(s) in common.", r.list1_index, r.list2_index, r.count);
}
// (0, 0) have 2 item(s) in common.
// (0, 1) have 2 item(s) in common.
// (0, 2) have 1 item(s) in common.
// (1, 0) have 2 item(s) in common.
// (1, 1) have 1 item(s) in common.
// (1, 2) have 2 item(s) in common.

OTHER TIPS

List<int[]> list1 = new List<int[]>() { new int[4] { 1, 2, 3, 4 }, new int[4] { 1, 2, 3, 5 } };
List<int[]> list2 = new List<int[]>() { new int[2] { 1, 2 }, new int[2] { 3, 4 }, new int[2] { 3, 5 } };

var results = list1.Select(x => list2.Select(y => y.Intersect(x).Count()).ToList()).ToList();

Result contains following data: [ [ 2, 2, 1 ], [ 2, 1, 2 ] ]

You can use Enumerable.Intersect to find out common items of first found in second list.

var commonList = list1.Intersect(list2);

The intersection of two sets A and B is defined as the set that contains all the elements of A that also appear in B, but no other elements

Edit Since you have array as element of list you have to go through each list item.

 List<int[]> list1 = new List<int[]>() { new int[4] { 1, 2, 3, 4 }, new int[4] { 1, 2, 3, 5 } };
 List<int[]> list2 = new List<int[]>() { new int[2] { 1, 2 }, new int[2] { 3, 4 }, new int[2] { 3, 5 } };
 List<int[]> list3 = new List<int[]>();
 for (int i = 0; i < list1.Count; i++)
 {
    list3.Add(list1[i].Intersect(list2[i]).ToArray()); 
 }
var commons = list1.Select(x => list2.Select(x.Intersect).ToArray()).ToArray();

Console.WriteLine(commons[0][0]); // Commons between list1[0] and list2[0]
Console.WriteLine(commons[0][1]); // Commons between list1[0] and list2[1]
Console.WriteLine(commons[3][0]); // Commons between list1[3] and list2[0]

Console.WriteLine(commons[3][0].Length); // Number of commons between [3] and [0]

Accourding to ur requirement, I think in C# there isnt such inbuild function in list which do exactly what you want but following function return exactly what you required in resultList. Hope this help you.

    private List<int> MatchList()
    {
        List<int[]> list1 = new List<int[]>() { new int[4] { 1, 2, 3, 4 }, new int[4] { 1, 2, 3, 5 } };
        List<int[]> list2 = new List<int[]>() { new int[2] { 1, 2 }, new int[2] { 3, 4 }, new int[2] { 3, 5 } };
        List<int> resultList = new List<int>();

        for (int i = 0; i < list1.Count; i++)
        {
            for (int j = 0; j < list2.Count; j++)
            {
                if (i == j)
                {
                    int result = 0;

                    foreach (int list1Element in list1[i])
                    {
                        foreach (int list2Element in list2[j])
                        {
                            if (list1Element == list2Element)
                            {
                                result +=1;
                            }
                        }
                    }

                    resultList.Add(result);
                }
            }
        }
        return resultList;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top