Sha256: e97dd73c23242eb0231cc4722de26830e58585a536a8aee916e333f1b5d31e94

Contents?: true

Size: 516 Bytes

Versions: 6

Compression:

Stored size: 516 Bytes

Contents

class Array

  # Returns a list of non-unique elements
  #
  #   [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!
    h1 = {}
    h2 = {}
    each {|i|
      h2[i] = true if h1[i]
      h1[i] = true
    }
    self.replace(h2.keys)
  end

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

  alias_method :duplicates, :nonuniq

end

Version data entries

6 entries across 5 versions & 1 rubygems

Version Path
facets-2.9.2 lib/core/facets/array/nonuniq.rb
facets-2.9.2 src/core/facets/array/nonuniq.rb
facets-2.9.1 lib/core/facets/array/nonuniq.rb
facets-2.9.0 lib/core/facets/array/nonuniq.rb
facets-2.9.0.pre.2 lib/core/facets/array/nonuniq.rb
facets-2.9.0.pre.1 lib/core/facets/array/nonuniq.rb