Skip to content

Instantly share code, notes, and snippets.

@scaint
Last active August 29, 2015 14:20
Show Gist options
  • Save scaint/36ae892dea41defd7f1b to your computer and use it in GitHub Desktop.
Save scaint/36ae892dea41defd7f1b to your computer and use it in GitHub Desktop.
input_array = [1, 2, 1, 4, 1.0, 1r]
require 'set'
Set.new(input_array).to_a
# => [1, 2, 4, 1.0, (1/1)]
unique_elements = []
input_array.each { |el| unique_elements << el unless unique_elements.include?(el) }
unique_elements
# => [1, 2, 4]
unique_elements = {}
input_array.each { |el| unique_elements[el.hash] = el unless unique_elements.key?(el.hash) }
unique_elements.values
# => [1, 2, 4, 1.0, (1/1)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment