Determine if one array contains all elements of another array

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

    Here's a naive and straightforward implementation (not the most efficient one, likely). Just count the elements and compare both elements and their occurrence counts.

    class Array
      def contains_all? ary
        # group the arrays, so that 
        #   [2, 1, 1, 3] becomes {1 => 2, 2 => 1, 3 => 1}
        my_groups = group_and_count self
        their_groups = group_and_count ary
    
        their_groups.each do |el, cnt|
          if !my_groups[el] || my_groups[el] < cnt
            return false
          end
        end
    
        true
      end
    
      private
      def group_and_count ary
        ary.reduce({}) do |memo, el|
          memo[el] ||= 0
          memo[el] += 1
          memo
        end
      end
    
    end
    
    [1, 2, 3].contains_all? [1, 2]   # => true
    [1, 2, 3].contains_all? [1, 2, 2] # => false
    [2, 1, 2, 3].contains_all? [1, 2, 2] # => true
    [1, 2, 3].contains_all? [] # => true
    [].contains_all? [1, 2] # => false
    

提交回复
热议问题