Determine if one array contains all elements of another array

前端 未结 8 1882
闹比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:18

    class Array
      def contains_all? other
        other = other.dup
        each{|e| if i = other.index(e) then other.delete_at(i) end}
        other.empty?
      end
    end
    

提交回复
热议问题