I'm solving some problems on codewars while learning Ruby. I solved this one in a more C++-ish way (I'm still new to Ruby)
Then I came to check the best solution based on upvotes, which is:
def unique_in_order(iterable)
(iterable.is_a?(String) ? iterable.chars : iterable).chunk { |x| x }.map(&:first)
end
I don't know what the (&:first)
is. I know what map does and it seems when I run:
[1, 2, 3, 4, 4, 5].chunk {|x| x}.map(&:first)
the duplicate elements are removed.