Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to decide if List contains duplicates?

Tags:

scala

One way is this

list.distinct.size != list.size 

Is there any better way? It would have been nice to have a containsDuplicates method

like image 482
Jus12 Avatar asked Oct 07 '11 19:10

Jus12


People also ask

How do I find the number of duplicates in a list?

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.


1 Answers

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) 
like image 161
timday Avatar answered Sep 25 '22 06:09

timday