The opposite of Intersect()

本文关键字:Intersect of opposite The | 更新日期: 2023-09-27 17:49:35

Intersect可用于查找两个集合之间的匹配,如下所示:

// Assign two arrays.
int[] array1 = { 1, 2, 3 };
int[] array2 = { 2, 3, 4 };
// Call Intersect extension method.
var intersect = array1.Intersect(array2);
// Write intersection to screen.
foreach (int value in intersect)
{
    Console.WriteLine(value); // Output: 2, 3
}

然而,我想实现的是相反的,我想列出一个集合中缺少的项::

// Assign two arrays.
int[] array1 = { 1, 2, 3 };
int[] array2 = { 2, 3, 4 };
// Call "NonIntersect" extension method.
var intersect = array1.NonIntersect(array2); // I've made up the NonIntersect method
// Write intersection to screen.
foreach (int value in intersect)
{
    Console.WriteLine(value); // Output: 4
}

The opposite of Intersect()

如前所述,如果您希望得到4作为结果,您可以这样做:

var nonintersect = array2.Except(array1);

如果你想要真正的非相交(也包括1和4),那么这个应该可以做到:

var nonintersect = array1.Except(array2).Union( array2.Except(array1));

这不是性能最好的解决方案,但对于小列表,它应该工作得很好。

可以使用

a.Except(b).Union(b.Except(a));

也可以用

var difference = new HashSet(a);
difference.SymmetricExceptWith(b);

此代码仅枚举每个序列一次,并使用Select(x => x)隐藏结果以获得干净的linq风格扩展方法。由于它使用HashSet<T>,如果散列分布良好,则运行时间为O(n + m)。两个列表中的重复元素将被省略。

public static IEnumerable<T> SymmetricExcept<T>(this IEnumerable<T> seq1,
    IEnumerable<T> seq2)
{
    HashSet<T> hashSet = new HashSet<T>(seq1);
    hashSet.SymmetricExceptWith(seq2);
    return hashSet.Select(x => x);
}

我想你可能在找Except:

Except操作符生成集合两个序列之间的差异。它只返回第一个元素吗序列中没有出现的第二。您可以选择提供你自己的相等比较函数。

查看这个链接,这个链接,或者谷歌,了解更多信息

array1.NonIntersect(array2);

Linq中不存在非相交操作符,您应该执行

except -> union -> except

a.except(b).union(b.Except(a));

我不是100%确定你的非相交方法应该做什么(关于集合理论)-它是
B ' A (B中没有出现在A中的一切)?
如果是,那么您应该能够使用Except操作(B.Except(A))。

/// <summary>
/// Given two list, compare and extract differences
/// http://stackoverflow.com/questions/5620266/the-opposite-of-intersect
/// </summary>
public class CompareList
{
    /// <summary>
    /// Returns list of items that are in initial but not in final list.
    /// </summary>
    /// <param name="listA"></param>
    /// <param name="listB"></param>
    /// <returns></returns>
    public static IEnumerable<string> NonIntersect(
        List<string> initial, List<string> final)
    {
        //subtracts the content of initial from final
        //assumes that final.length < initial.length
        return initial.Except(final);
    }
    /// <summary>
    /// Returns the symmetric difference between the two list.
    /// http://en.wikipedia.org/wiki/Symmetric_difference
    /// </summary>
    /// <param name="initial"></param>
    /// <param name="final"></param>
    /// <returns></returns>
    public static IEnumerable<string> SymmetricDifference(
        List<string> initial, List<string> final)
    {
        IEnumerable<string> setA = NonIntersect(final, initial);
        IEnumerable<string> setB = NonIntersect(initial, final);
        // sum and return the two set.
        return setA.Concat(setB);
    }
}
string left = "411329_SOFT_MAC_GREEN";
string right= "SOFT_MAC_GREEN";
string[] l = left.Split('_');
string[] r = right.Split('_');
string[] distinctLeft = l.Distinct().ToArray();
string[] distinctRight = r.Distinct().ToArray();
var commonWord = l.Except(r, StringComparer.OrdinalIgnoreCase)
string result = String.Join("_",commonWord);
result = "411329"