possible_numbers = (0..9).to_a gets.to_i.times do |i| array = gets.chomp.split.map { |val| val =~ /\d/ ? val.to_i : val } case array[4] when 'YES' possible_numbers &= array when 'NO' possible_numbers -= array end end puts possible_numbers