I need to make intersection of n-arrays with millions of elements (database ID's). This code works perfect, but slow (with very big arrays). How can i improve it?
[[1,2,3,4],[2,4,6,8],[4,5,8]].inject([]){|c,v| c = v if c.size==0; c = c&v if c.size>0; c }
[1,2,3,4] & [2,4,6,8] & [4,5,8] #=> [4]
The intersection method uses hash so it should be quick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With