检查一个数组是否是Ruby中另一个数组的子集 [英] Check if an array is subset of another array in Ruby
本文介绍了检查一个数组是否是Ruby中另一个数组的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
无论元素的顺序如何,如何检查一个数组是否是另一个数组的子集?
a1 = [3, 6, 4]a2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]……?a1 是 a2 的子集
解决方案
使用集合.然后你可以使用 set.subset?
.示例:
需要'设置'a1 = 设置 [3,6,4]a2 = 集合[1,2,3,4,5,6,7,8,9]put a1.subset?(a2)
输出:
<前>真的在线查看:ideone
How can I check whether one array is a subset of another array, regardless of the order of elements?
a1 = [3, 6, 4]
a2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
...?
a1 is a subset of a2
解决方案
Use sets. Then you can use set.subset?
. Example:
require 'set'
a1 = Set[3,6,4]
a2 = Set[1,2,3,4,5,6,7,8,9]
puts a1.subset?(a2)
Output:
true
See it working online: ideone
这篇关于检查一个数组是否是Ruby中另一个数组的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文