array in array, like include?

B

Ben

Is there a function to checking to see if the elements of one array
exist in another?

a = [1,2,3,4,5]
b = [1,4]

a.include?(b) will only work if b is a string. I'd like to get a
true/false if b exists in a.

Thanks!
Ben
 
J

Joel VanderWerf

Ben said:
Is there a function to checking to see if the elements of one array
exist in another?

a = [1,2,3,4,5]
b = [1,4]

a.include?(b) will only work if b is a string. I'd like to get a
true/false if b exists in a.

Thanks!
Ben

require 'set'

[1,4].to_set.subset?([1,2,3,4,5].to_set)

=> true
 
J

Joe Cheng

Ben said:
Is there a function to checking to see if the elements of one array
exist in another?

a = [1,2,3,4,5]
b = [1,4]

a.include?(b) will only work if b is a string. I'd like to get a
true/false if b exists in a.
How about:
(a & b).length == b.length
 
J

Joel VanderWerf

Joe said:
Ben said:
Is there a function to checking to see if the elements of one array
exist in another?

a = [1,2,3,4,5]
b = [1,4]

a.include?(b) will only work if b is a string. I'd like to get a
true/false if b exists in a.
How about:
(a & b).length == b.length

(a&[1,4,4]).length == [1,4,4].length
=> false

so it might or might not be ok...
 
C

Charles Steinman

Joe said:
Ben said:
Is there a function to checking to see if the elements of one array
exist in another?

a = [1,2,3,4,5]
b = [1,4]

a.include?(b) will only work if b is a string. I'd like to get a
true/false if b exists in a.
How about:
(a & b).length == b.length

That should be

==code==
(a & B).length != 0
==endcode==

Because & gives an array of the common members. So if they have no
common members, it will return a zero-length array, and a
nonzero-length array otherwise.
 
C

Charles Steinman

Charles said:
Joe said:
Ben said:
Is there a function to checking to see if the elements of one array
exist in another?

a = [1,2,3,4,5]
b = [1,4]

a.include?(b) will only work if b is a string. I'd like to get a
true/false if b exists in a.
How about:
(a & b).length == b.length

That should be

==code==
(a & B).length != 0
==endcode==

Because & gives an array of the common members. So if they have no
common members, it will return a zero-length array, and a
nonzero-length array otherwise.

Immediately realized that I completely misread the requirements. Sorry
for the incorrect correction. D'oh.
 
P

Pete Elmore

Ben said:
a = [1,2,3,4,5]
b = [1,4]

a.include?(b) will only work if b is a string. I'd like to get a
true/false if b exists in a.
The to_set solution is probably better, but you could do something like
class Array
def subset(s)
!s.find { |el| !self.include? el }
end
end

irb(main):018:0> [1, 2, 3, 4].subset([1, 2])
=> true
irb(main):019:0> [1, 2, 3, 4].subset([1, :seventy_five])
=> false
 
C

Charles Steinman

Pete said:
Ben said:
a = [1,2,3,4,5]
b = [1,4]

a.include?(b) will only work if b is a string. I'd like to get a
true/false if b exists in a.
The to_set solution is probably better, but you could do something like
class Array
def subset(s)
!s.find { |el| !self.include? el }
end
end

irb(main):018:0> [1, 2, 3, 4].subset([1, 2])
=> true
irb(main):019:0> [1, 2, 3, 4].subset([1, :seventy_five])
=> false

I benchmarked, and to_set came out way slower:

to_set.subset? another.to_set
0m19.089s

!another.find {|member| !include?(member)}
0m2.628s

another.all? {|member| include? member} # my solution
0m2.493s
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top