# Array question

Hi all,

Just wonder:

1)When substracting array2 from array1 to get the difference elements
between two arrays(array1-array2), must array1.size >= array2?

2)Is there mentod in Array class that can return the element only
unique to either of two arrays?

For example, array1=[1,2,3],array2=[1,4,7]
the return array is [2,3,4,7]

Thanks,

Li

On Tue, Sep 16, 2008 at 05:27:03AM +0900, Li Chen wrote:

Hi all,

Just wonder:

1)When substracting array2 from array1 to get the difference elements
between two arrays(array1-array2), must array1.size >= array2?

irb is great for this stuff.

[1, 2] - [1, 3, 4]
=> [2]

2)Is there mentod in Array class that can return the element only
unique to either of two arrays?

http://ruby-doc.org/core/classes/Array.html

For example, array1=[1,2,3],array2=[1,4,7]
the return array is [2,3,4,7]

There might be a better way, but I came up with:

([1, 2, 3] - [1, 4, 7]) + ([1, 4, 7] - [1, 2, 3])
=> [2, 3, 4, 7]

nathan
nathan_at_nathanpowell_dot_org

What kind of crazy nut would spend two or three hours a day just
running?
~ Steve Prefontaine

Li Chen wrote:
(…)

2)Is there mentod in Array class that can return the element only
unique to either of two arrays?

For example, array1=[1,2,3],array2=[1,4,7]
the return array is [2,3,4,7]

Thanks,

Li

It’s probably academic, but what is the desired result with these 2
arrays?
array1 = [1,2,2,3]
array2 = [1,4,7,7]

Nathan Powell’s method: [2, 2, 3, 4, 7, 7]
Rob B.'s method: [2, 3, 4, 7]
Perhaps desired result: [3,4] ?

Regards,

Siep

Nathan Powell wrote:

On Tue, Sep 16, 2008 at 05:27:03AM +0900, Li Chen wrote:

([1, 2, 3] - [1, 4, 7]) + ([1, 4, 7] - [1, 2, 3])
=> [2, 3, 4, 7]

Or:

(a1|a2) - (a1&a2)

Or:

require ‘set’
Set.new(a1) ^ Set.new(a2)

(I thought I remem

Joel VanderWerf wrote:

require ‘set’
Set.new(a1) ^ Set.new(a2)

(I thought I remem
bered that Array had a symmetric difference
operator, but apparently not.)

Joel VanderWerf wrote:
(…)

Or:

require ‘set’
Set.new(a1) ^ Set.new(a2)

I figured this out by typo, allthough it is in the documentation.
Anyway, you can do
Set.new(a1) ^ a2

if a2 is enumerable.

Regards,

Siep

Add the two arrays and call uniq on the new array

On Sep 15, 2008, at 4:42 PM, Nathan Powell wrote:

[1, 2] - [1, 3, 4]

## running? ~ Steve Prefontaine

irb> (a1 | a2) - (a1 & a2)
=> [2, 3, 4, 7]

-Rob

We’ll see.

On Tue, Sep 16, 2008 at 11:12:50PM +0900, Amos K. wrote:

Add the two arrays and call uniq on the new array

([1, 2, 3] + [1, 4, 7]).uniq
=> [1, 2, 3, 4, 7]

That doesn’t result in what he asked it to result in. I am not
convinced what I came up with was what he was really after either. I
think Siep was on to something.

nathan
nathan_at_nathanpowell_dot_org

What kind of crazy nut would spend two or three hours a day just
running?
~ Steve Prefontaine

On Tue, Sep 16, 2008 at 11:25:00PM +0900, Amos K. wrote:

Forgot to ask what he was looking for.

In the original email:

For example, array1=[1,2,3],array2=[1,4,7] the return array is [2,3,4,7]

It’s not entirely clear as one could arrive at that final array a couple
ways.

nathan
nathan_at_nathanpowell_dot_org

At this point, anyone proposing to run Windows on servers should be
prepared
to explain what they know about servers that Google, Yahoo, and Amazon
don’t.
~ Paul Graham

Case 1 and 2, maybe the desired result?

irb(main):088:0> def there_can_be_only_one(arr)
irb(main):089:1> arr.sort!.collect {|x| (arr[arr.index(x)] !=
arr[arr.index(x)+1] ? x:nil)}.compact
irb(main):090:1> end
=> nil
irb(main):091:0> there_can_be_only_one([1,2,3] + [1,4,7])
=> [2, 3, 4, 7]
irb(main):092:0> there_can_be_only_one([1,2,2,3] + [1,4,7,7])
=> [3, 4]
irb(main):093:0>

…assumes the elements to be compared are all the same type (Fixnums
can’t sort with Strings, eg)

Forgot to ask what he was looking for.

That only works if they are touching and that can be accomplished with
inject really easy.

([1,2,2,3]+[1,4,7,7]).sort…inject([]) { |final, e| fianl[-1] == e?
final[0…-2] : final << e }
#=> [3,4]

([1,2,3]+[1,4,7]).sort…inject([]) { |final, e| fianl[-1] == e?
final[0…-2] : final << e }
#=> [2,3,4,7]

although with the later one you can just do
([1,2,3]+[1,4,7]).uniq