ruby - determine if an array contains all elements of another array often enough? -
i have array available elements , array required elements. want find out if required elements available.
the arrays may contain "duplicates". there must many elements available required. first trial #contains? method fails because include? checks if element available @ least once
-- edit: simplified example code --
# first , simple trial # fails on duplicate elements class array def contains?(other) other.all? { |element| include?(element) } end end available = [1, 1, 1, 2, 2, 3] small = [1, 1, 2, 3] big = [1, 1, 2, 3, 3] available.contains?(small) # true intended available.contains?(big) # true should false # because "big" contains more "3s" "available"
def contains?(other) other.elements.group_by{|e| e}.all?{|e, a| elements.count(e) >= a.length} end
Comments
Post a Comment