Given you have a Ruby Array like a = [1,2,3,2,4,4,2,5]
how can you select elements that occurred multiple times in the given array ?
so return value == [2,4]
Given you have a Ruby Array like a = [1,2,3,2,4,4,2,5]
how can you select elements that occurred multiple times in the given array ?
so return value == [2,4]
a.group_by(&:itself).select{|_, a| a[1]}.keys
My colleague suggested this :
a = [1,2,3,2,4,4,2,5]
a.select{ |el| a.count(el) > 1 }.uniq
# => [2,4]