How to determine if one array contains all elements of another array

How to determine if one array contains all elements of another array

Asked on January 5, 2019 in Ruby on Rails.
Add Comment


  • 2 Answer(s)

    Use this below command: 

    a = [5, 1, 6, 14, 2, 8]
    b = [2, 6, 15]
     
    a - b
    => [5, 1, 14, 8]
     
    b - a
    => [15]
     
    (b - a).empty?
    => false
    

    Answered on January 5, 2019.
    Add Comment

    Easier to read: 

    a2.all? { |e| a1.include?(e) }
    
    

    Array intersection: 

    (a1 & a2).size == a1.size
    
    

    The Size is used  for speed, 

    (a1 & a2) == a1
    
    

    Answered on January 5, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.