Sha256: 2a22134d52aee0a7c748f4729cf9062e532fd53592e49823f9dfa545bb7d8fd5

Contents?: true

Size: 595 Bytes

Versions: 10

Compression:

Stored size: 595 Bytes

Contents

module Enumerable

  # Returns a list on non-unique,
  #
  #   [1,1,2,2,3,4,5].nonuniq  #=> [1,2]
  #
  # CREDIT: Martin DeMello

  def nonuniq
    h1 = {}
    h2 = {}
    each {|i|
      h2[i] = true if h1[i]
      h1[i] = true
    }
    h2.keys
  end

  #   #
  #   def nonuniq!
  #     raise unless respond_to?(:replace)
  #     h1 = {}
  #     h2 = {}
  #     each {|i|
  #       h2[i] = true if h1[i]
  #       h1[i] = true
  #     }
  #     self.replace(h2.keys)
  #   end

  # Return list of dulicate elements.
  #
  # CREDIT: Thibaut Barrère

  alias_method :duplicates, :nonuniq

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/enumerable/duplicates.rb
facets-2.8.3 lib/core/facets/enumerable/duplicates.rb
facets-2.8.2 lib/core/facets/enumerable/duplicates.rb
facets-2.8.1 lib/core/facets/enumerable/duplicates.rb
facets-2.8.0 lib/core/facets/enumerable/duplicates.rb
facets-2.7.0 lib/core/facets/enumerable/duplicates.rb
facets-2.6.0 lib/core/facets/enumerable/duplicates.rb
facets-2.5.1 lib/core/facets/enumerable/duplicates.rb
facets-2.5.0 lib/core/facets/enumerable/duplicates.rb
facets-2.5.2 lib/core/facets/enumerable/duplicates.rb