问题描述
一种方法是这样
list.distinct.size != list.size
有没有更好的办法?如果有一个 containsDuplicates
方法
Is there any better way? It would have been nice to have a containsDuplicates
method
推荐答案
假设更好"意味着更快",请参阅 这个问题,这似乎显示了一些更快的方法(尽管请注意 distinct 使用 HashSet 并且已经是 O(n)).YMMV 当然,取决于特定的测试用例,scala 版本等. 可能对distinct.size"方法的任何显着改进都来自于发现重复项后立即退出,但有多少加速是实际获得的数据在很大程度上取决于您的用例中实际重复的常见程度.
Assuming "better" means "faster", see the alternative approaches benchmarked in this question, which seems to show some quicker methods (although note that distinct uses a HashSet and is already O(n)). YMMV of course, depending on specific test case, scala version etc. Probably any significant improvement over the "distinct.size" approach would come from an early-out as soon as a duplicate is found, but how much of a speed-up is actually obtained would depend strongly on how common duplicates actually are in your use-case.
如果您的意思是更好",因为您想编写 list.containsDuplicates
而不是 containsDuplicates(list)
,请使用隐式:
If you mean "better" in that you want to write list.containsDuplicates
instead of containsDuplicates(list)
, use an implicit:
implicit def enhanceWithContainsDuplicates[T](s:List[T]) = new {
def containsDuplicates = (s.distinct.size != s.size)
}
assert(List(1,2,2,3).containsDuplicates)
assert(!List("a","b","c").containsDuplicates)
这篇关于确定 List 是否包含重复项的最简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!