How to Check If a Ruby Array Includes One of Several Values

How can I check if a Ruby array includes one of several values?

Set intersect them:

a1 & a2

Here's an example:

> a1 = [ 'foo', 'bar' ]
> a2 = [ 'bar', 'baz' ]
> a1 & a2
=> ["bar"]
> !(a1 & a2).empty? # Returns true if there are any elements in common
=> true

Array.include? multiple values

You could take the intersection of two arrays, and see if it's not empty:

([2, 6, 13, 99, 27] & [2, 6]).any?

How do you check if an array's values includes one or multiple values?

EDIT: I endorse Mark Thomas' alternate solution that uses the core Set class.

While my solution more strictly answers the question at hand of how to do this with arrays, sjsc may benefit from reviewing his own case and exploring the option of using sets instead.

There are plenty of valid reasond to use arrays (maintaining order, allowing for duplicates), for which the below still suffices, but if none of these are involved, sjsc might actually benefit from using Set instead of Array, and to that extent, Mark's solution is semantically superior.


I don't know of any library method that does this, but it wouldn't be too hard to write your own function.

class Array
def subset?(a)
(self - a).length == 0
end
end

I'm sure there are computationally more efficient ways to accomplish this, but this should do what you're looking for.

Doing array intersection works and basically amounts to the same thing.

class Array
def subset?(a)
(self & a).length == length
end
end

Optimization at this level isn't going to help matters too much, but what you don't want to do is start comparing arrays multiple times:

class Array
# don't do this
def subset?(a)
(self & a) == a
end
end

Check for multiple items in array using .include? -- Ruby Beginner

Using set intersections (Array#:&):

(myarray & ["val1", "val2", "val3", "val4"]).present?

You can also loop (any? will stop at the first occurrence):

myarray.any? { |x| ["val1", "val2", "val3", "val4"].include?(x) }

That's ok for small arrays, in the general case you better have O(1) predicates:

values = ["val1", "val2", "val3", "val4"].to_set
myarray.any? { |x| values.include?(x) }

With Ruby >= 2.1, use Set#intersect:

myarray.to_set.intersect?(values.to_set)

How to determine if one array contains all elements of another array


a = [5, 1, 6, 14, 2, 8]
b = [2, 6, 15]

a - b
# => [5, 1, 14, 8]

b - a
# => [15]

(b - a).empty?
# => false

How to check if a value exists in an array in Ruby

You're looking for include?:

>> ['Cat', 'Dog', 'Bird'].include? 'Dog'
=> true

Ruby - Checking if multiple values appear anywhere in an array

You can use array intersection:

array1 = ["key1", "key2", "key3", "sword", "dagger"]
array2 = ["key1", "key2", "key3"]

puts (array1 & array2 == array2) ? "it does" : "it doesn't"
#=> "it does"

array2 = ["key1", "key2", "cat"]
puts (array1 & array2 == array2) ? "it does" : "it doesn't"
#=> "it doesn't"

or difference:

puts (array2 - array1).empty? ? "it does" : "it doesn't"

How can I do a strict check on if a Ruby array contains only certain values?

Simple subtraction will provide you desired output,

def compare(checked_array, standard)
(checked_array - standard).empty?
end

Array include any value from another array?


(cheeses & foods).empty?

As Marc-André Lafortune said in comments, & works in linear time while any? + include? will be quadratic. For larger sets of data, linear time will be faster. For small data sets, any? + include? may be faster as shown by Lee Jarvis' answer -- probably because & allocates a new Array while another solution does not and works as a simple nested loop to return a boolean.



Related Topics



Leave a reply



Submit