One way is this
list.distinct.size != list.size
Is there any better way? It would have been nice to have a containsDuplicates
method
If you want to count duplicates for a given element then use the count() function. Use a counter() function or basics logic combination to find all duplicated elements in a list and count them in Python.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With