Answers for "ruby array with unique values"

0

ruby array with unique values

require 'set'

array = ['foo', :bar, 'foo', :foobar]
# Sets are much faster than arrays
# when it comes to checking for inclusion,
# intersections etc
set = array.to_set #=> #<Set: {"foo", :bar, :foobar}>
array = set.to_a
p array #=> ["foo", :bar, :foobar]
Posted by: Guest on April-20-2022
0

ruby array with unique values

array = ['foo', :bar, 'foo', :foobar]
array.uniq #=> ["foo", :bar, :foobar]
Posted by: Guest on April-20-2022

Code answers related to "ruby array with unique values"

Browse Popular Code Answers by Language