removing duplicates in array of arrays in Ruby -
i have array of arrays, this:
aa = [ [a,d], [a,d1], [a,d], [b,d], [b,d2], [b,d3], [b,d2], [a,d2] ]
i have unique array of arrays, not on first element - can doing aa.uniq(&:first) - rather remove inner arrays if both values match. result be:
aa = [ [a,d], [a,d1], [a,d2], [b,d], [b,d2], [b,d3] ]
can assist in pointing me efficient way of doing this? have large nr of arrays - in order of 1 million - need process.
any appreciated! john
if need maintain collection of elements where each element unique , order not important. should use set. instance,
require 'set' my_set = set.new my_set << [1, 'a'] my_set << [1, 'a'] my_set << [1, 'b'] my_set.each { |elem| puts "#{elem}" }
it give you
[1, "a"] [1, "b"]
if order important, use uniq!
on array
aa.uniq!
Comments
Post a Comment