Hi
I have a handful of arrays, and I want to see if any of the values in any of the arrays, appear in any of the other arrays, is there a clean way to do this or would it be a case of comparing them one at a time?
Hi
I have a handful of arrays, and I want to see if any of the values in any of the arrays, appear in any of the other arrays, is there a clean way to do this or would it be a case of comparing them one at a time?
Add all the elements of the arrays into a hash and count the times you add each element.
one = [1, 2, 3, ]
two = [2, 3, 4, ]
three = [3, 4, 5, ]
arr = [one, two, three]
ans =
arr.reduce(Hash.new(0)){
|acc, e|
e.each{|e| acc[e] += 1}
acc
}
p ans
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs