# Merge two arrays?

Hi all

What’s the easiest way to merge two arrays?

Thanks a lot,
Josh

On 1/12/06, Joshua M. [email protected] wrote:

What’s the easiest way to merge two arrays?

It really depends on what you mean by merge.

You could do stuff like:

[1,2] + [2,3] #=> [1,2,2,3]
[1,2] | [2,3] #=> [1,2,3]

Some motivation for the question might help use in giving you good
pointers.

Brian.

This already anwers my question, thank you.

On 1/12/06, Joshua M. [email protected] wrote:

array1 = [1,2,4,6]
array2 = [1,3,5,6]

Is there an easy way for doing this?

array1 ?? array2 = [1,6] # Only add entries that occur in both arrays
array1 ?? array2 = [2,3,4,5] # Only add entries that occur in one of
both arrays

array1 & array2 #=> [1,6]

the second is harder but you could do it like:
(array1 + array2) - (array1 & array2) #=> [2,3,4,5]

I am not sure if that can be made simpler but it should work.

Brian.

Thanks a lot.

In Message-Id: [email protected]
Joshua M. [email protected] writes:

array1 = [1,2,4,6]
array2 = [1,3,5,6]

array1 ?? array2 = [1,6] # Only add entries that occur in both arrays
array1 ?? array2 = [2,3,4,5] # Only add entries that occur in one of
both arrays

For former Array#& can be used if there’re no duplicated elements in
your arrays. For latter, hmm, I can’t remember no one methods but you
can (array1|array2)-(array1&array2), on same assumption.

Note these operators always make a new array for a result, they may
not be suitable when both your arrays are too large, though

Joshua M. wrote:

This already anwers my question, thank you.

Hrm oke, got another related question.

array1 = [1,2,4,6]
array2 = [1,3,5,6]

Is there an easy way for doing this?

array1 ?? array2 = [1,6] # Only add entries that occur in both arrays
array1 ?? array2 = [2,3,4,5] # Only add entries that occur in one of
both arrays

YANAGAWA Kazuhisa wrote:

For former Array#& can be used if there’re no duplicated elements in
your arrays. For latter, hmm, I can’t remember no one methods but you
can (array1|array2)-(array1&array2), on same assumption.

Note these operators always make a new array for a result, they may
not be suitable when both your arrays are too large, though

be careful with ‘’&’ and ‘-’
http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/65361

For former Array#& can be used if there’re no duplicated elements in

Hrm I don’t really got this… Where do I have to put the references
(array1, array2) for this?

Note these operators always make a new array for a result, they may
not be suitable when both your arrays are too large, though

Thanks for the hint. But they don’t create clones of the objects in the
arrays, they just copy the references, right?

Quoting Joshua M. [email protected]:

array1 ?? array2 = [1,6] # Only add entries that occur in both
arrays

array1 & array2

Here, you’re finding the intersection of two sets.

array1 ?? array2 = [2,3,4,5] # Only add entries that occur in one
of both arrays

There are at least two ways to do this:

( array1 | array2 ) - ( array1 & array2 )

or

( array1 - array2 ) | ( array2 - array1 )

Some people call this “exclusion”; it’s the inverse of intersection.

However, at this point you might want to consider using Set rather
than Array. Performing set operations on Arrays is not very
efficient.

To use Set, require ‘set’. You can create a set like:

Set[1, 3, 5, 6]

or (from an array):

Set[*array]

The set operations union, intersection, and difference (|, &, -)
work the same for sets as they do arrays, and Set also implements
Enumerable (so you get Set#each, Set#to_a, etc…).

Ah, one additional thing – if you want exclusion, there is a
shorter way to write it with sets:

set1 = Set[1, 2, 4, 6]
set2 = Set[1, 3, 5, 6]

set1 ^ set2 # => #<Set: {5, 2, 3, 4}>

Note that sets are unordered containers.

-mental

In Message-Id: [email protected]
Joshua M. [email protected] writes:

Hrm I don’t really got this… Where do I have to put the references
(array1, array2) for this?

Note these operators always make a new array for a result, they may
not be suitable when both your arrays are too large, though

Thanks for the hint. But they don’t create clones of the objects in the
arrays, they just copy the references, right?

That’s right. By above saying I want to note on memory utilization
issue:

Since very_huge_array (operator) another_very_huge_array possibly
yields yet_another_very_huge_array, (a1|a2)-(a1&a2) can creates two
huge arrays which immediately discarded after the computation.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.