Issue #15590 has been updated by xdmx (Eric Bloom).


> I assume that you more refer to a complementary method to .uniq but I am not
> completely sure, so I hope it's ok for you to clarify on that just to make sure, when
> you have some time.

Sorry for not having included an example!

Yes, I mean it as a complementary method of `uniq`, and actually `duplicates` would be much better than `dups` :)

> Could you elaborate the use case?

The use case is mostly: you have a list of data, which could be a list of ids, names, codes, or others and you want to know which ones of them are duplicated in the array.

So for example, you have a list of cities: `["Tokyo", "Paris", "London", "Miami", "Paris", "Orlando", "Dubai", "Tokyo", "Paris"]` and it includes some duplicated values (`Tokyo` and `Paris`), and you want to find out which ones are duplicated. This would be the same for ids, or other values. If you increase the list to hundreds of values, it'd be harder to find it by just looking at the list :)

As the result I'd probably expect the list each duplicated values (`["Paris", "Tokyo", "Paris"]`) instead of a uniq version of them (`["Paris", "Tokyo"]`).

I personally do it many times to check data in the database or from other sources (csv, json) to discover duplicated records with the same name, code, or other values, especially while cleaning up legacy data and where there were no previous constraints/checks.

----------------------------------------
Feature #15590: Add dups to Array to find duplicates
https://bugs.ruby-lang.org/issues/15590#change-76692

* Author: xdmx (Eric Bloom)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Many times I find myself debugging data and the need of finding duplicated values inside of an Array.

Based on the amount of data it could be a simple `array.detect { |value| array.count(value) > 1 }` or a more performant way like

```ruby
def dups_for(array)
  duplicated_values = []
  tmp = {}
  array.each do |value|
    duplicated_values << value if tmp[value]
    tmp[value] = true
  end
  duplicated_values
end
```
 
It would be awesome if there was a way directly from the core language to call `dups` (or another name, as it could be too similar to the current `dup`) on an array in order to get all the duplicated values.

I'd love to create a PR for this, but my C level is non-existent 




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>