Determine if one array contains all elements of another array

前端 未结 8 1875
闹比i
闹比i 2021-02-07 12:05

I need to tell if an array contains all of the elements of another array with duplicates.

[1,2,3].contains_all? [1,2]   #=> true
[1,2,3].contains_all         


        
8条回答
  •  南方客
    南方客 (楼主)
    2021-02-07 12:37

    It seems you need a multiset. Check out this gem, I think it does what you need.

    You can use is and do something like (if the intersection is equal to the second multiset then the first one includes all of its elements):

    @ms1 & @ms2 == @ms2
    

提交回复
热议问题