C # Similarities of Two Arrays

There should be a better way to do this, I'm sure ...

// Simplified code
var a = new List<int>() { 1, 2, 3, 4, 5, 6 };
var b = new List<int>() { 2, 3, 5, 7, 11 };
var z = new List<int>();
for (int i = 0; i < a.Count; i++)
    if (b.Contains(a[i]))
        z.Add(a[i]);
// (z) contains all of the numbers that are in BOTH (a) and (b), i.e. { 2, 3, 5 }

I do not mind using the above technique, but I want something fast and efficient (I need to compare very large lists <> several times), and this is neither one nor the other! Any thoughts?

Edit: since it matters - I am using .NET 4.0, the original arrays are already sorted and do not contain duplicates.

+5
source share
5 answers

You can use IEnumerable.Intersect.

var z = a.Intersect(b);

which is likely to be more effective than your current solution.

, - . , , - .

Edit :

- , .

    int j = 0;
    foreach (var i in a)
    {
        int x = b[j];
        while (x < i)
        {
            if (x == i)
            {
                z.Add(b[j]);
            }
            j++;
            x = b[j];
        }
    }

;)

Edit - , Linq SortedList , , . , .

, -, , . :

        int j = 0;
        int b1 = b[j];
        foreach (var a1 in a)
        {
            while (b1 <= a1)
            {
                if (b1 == a1)
                    z1.Add(b[j]);
                j++;
                if (j >= b.Count)
                    break;
                b1 = b[j];
            }
        }
+6

IEnumerable.Intersect, , , .

, Set, , . , z, , .

var set = new HashSet<int>(a);
var z = new List<int>(Math.Min(set.Count, b.Count));

foreach(int i in b)
{
    if(set.Contains(i))
        a.Add(i);
}

O (N + M) (N M - ).

set.IntersectWith(b), , , 100%.

+2

Intersect() . MSDN:

, .

So in your case:

var z = a.Intersect(b);
+1
source

If you can use LINQ, you can use the extension method Enumerable.Intersect().

+1
source

Use SortedSet<T>in namespace System.Collections.Generic:

SortedSet<int> a = new SortedSet<int>() { 1, 2, 3, 4, 5, 6 };
SortedSet<int> b = new SortedSet<int>() { 2, 3, 5, 7, 11 };
b.IntersectWith(s2);

But you have no duplicates! Although your second list should not be SortedSet. It can be any collection ( IEnumerable<T>), but inside the method acts in such a way that if the second list is also SortedSet<T>, the operation is an O (n) operation.

+1
source

All Articles