Project

General

Profile

Actions

Feature #15590

open

Add dups to Array to find duplicates

Added by xdmx (Eric Bloom) almost 6 years ago. Updated almost 6 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:91424]

Description

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

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 😞

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0