I have two arrays and i want to get all items which are NOT common to both, so basically if i have two arrays A and B, i want all elements in A that are not in B. what is the best way to do this apart from using two nested loops. thanks rahtha -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Feb-16 08:59 UTC
Re: best way to select uncommon elements from array
On Feb 16, 7:10 am, rahtha <rahul.that...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have two arrays and i want to get all items which are NOT common to > both, so basically if i have two arrays A and B, i want all elements > in A that are not in B. what is the best way to do this apart from > using two nested loops. >A - B ? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Tue, Feb 16, 2010 at 8:10 AM, rahtha <rahul.thathoo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have two arrays and i want to get all items which are NOT common to > both, so basically if i have two arrays A and B, i want all elements > in A that are not in B. what is the best way to do this apart from > using two nested loops.But do you want the ones in B that are not in A as well? (symmetric difference) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Nope don''t care about that case just the ones in A that are not in B On Feb 16, 8:02 am, Xavier Noria <f...-xlncskNFVEJBDgjK7y7TUQ@public.gmane.org> wrote:> On Tue, Feb 16, 2010 at 8:10 AM, rahtha <rahul.that...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I have two arrays and i want to get all items which are NOT common to > > both, so basically if i have two arrays A and B, i want all elements > > in A that are not in B. what is the best way to do this apart from > > using two nested loops. > > But do you want the ones in B that are not in A as well? (symmetric difference)-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Aldric Giacomoni
2010-Feb-16 18:09 UTC
Re: best way to select uncommon elements from array
rahtha wrote:> Nope don''t care about that case just the ones in A that are not in BWell, then it''s what Frederick said. irb(main):065:0> a = [1,2,3] => [1, 2, 3] irb(main):066:0> b = [2,4,6] => [2, 4, 6] irb(main):067:0> a-b => [1, 3] -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.