I have implemented my own version of Ruby's Array uniq-method (Ruby docs - Class array) as a monkey patch on Array.
Method-impl.:
require 'set'
class Array
def own_uniq
uniq = Set.new
self.each { uniq.add(it) }
uniq.to_a
end
end
Usage:
require_relative "own_uniq.rb"
example = [1, 2, 4, 8, 16, 32, 1, 4, 16]
puts example.own_uniq.inspect # -> [1, 2, 4, 8, 16, 32]
Although it seems to work: Is my implementation generally correct?
Can it be improved? Respectively: Is there a better approach?