检查列表是否不相交

我需要检查两个列表是否有任何共同元素。我只需要是/否- 我不需要常见元素的实际列表。

我可以使用,Enumerable.Intersect()但这实际上返回匹配项的集合,这似乎需要额外的开销。有没有更好的方法来检查列表是否不相交?


我的列表确实碰巧是,List<T>但这并不重要,HashSet如果这样更方便的话,我可以使用类似(比如说)这样的东西。即,我不想不必要地限制潜在的解决方案。



临摹微笑
浏览 174回答 1
1回答

函数式编程

最简单的版本(使用 Intersect):&nbsp;public bool Compare(List<T> firstCollection, List<T> secondCollection)&nbsp;{&nbsp; &nbsp; return firstCollection.Intersect(secondCollection).Any();&nbsp;}唯一的警告是在调用中T实现IEquatable<T>或传递自定义。还要确保与IEqualityComparer<T>IntersectGetHashCodeEquals编辑1:这个使用 Dictionary 的版本不仅会提供boolean比较,还会提供元素。在这个解决方案Dictionary中,最终将包含与交叉元素数量相关的数据,其中一个集合中的元素数量而不是另一个集合中的元素数量,因此相当耐用。这个解决方案也有IEquatable<T>要求public bool CompareUsingDictionary(IEnumerable<T> firstCollection, IEnumerable<T> secondCollection)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Implementation needs overiding GetHashCode methods of the object base class in the compared type&nbsp; &nbsp; &nbsp; &nbsp; // Obviate initial test cases, if either collection equals null and other doesn't then return false. If both are null then return true.&nbsp; &nbsp; &nbsp; &nbsp; if (firstCollection == null && secondCollection != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; if (firstCollection != null && secondCollection == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; if (firstCollection == null && secondCollection == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; // Create a dictionary with key as Hashcode and value as number of occurences&nbsp; &nbsp; &nbsp; &nbsp; var dictionary = new Dictionary<int, int>();&nbsp; &nbsp; &nbsp; &nbsp; // If the value exists in first list , increase its count&nbsp; &nbsp; &nbsp; &nbsp; foreach (T item in firstCollection)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get Hash for each item in the list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int hash = item.GetHashCode();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If dictionary contains key then increment&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dictionary.ContainsKey(hash))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dictionary[hash]++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Initialize he dictionary with value 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dictionary.Add(hash, 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // If the value exists in second list , decrease its count&nbsp; &nbsp; &nbsp; &nbsp; foreach (T item in secondCollection)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get Hash for each item in the list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int hash = item.GetHashCode();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If dictionary contains key then decrement&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dictionary.ContainsKey(hash))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dictionary[hash]--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Check whether any value is 0&nbsp; &nbsp; &nbsp; &nbsp; return dictionary.Values.Any(numOfValues => numOfValues == 0);&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP